多端适配
组件把重复的界面(商品卡片、导航栏、弹窗)封装起来,页面引用时传 props 就能复用。这是工程化的关键。
自定义组件
components/ 目录下建 .vue 文件;props 接收外部数据;$emit 向父组件发事件;页面 import 注册使用。
easycom
uni-app 的 easycom 规则:组件放 components/组件名/组件名.vue,页面直接用标签,无需 import 注册,自动引入。
代码示例
<!-- components/product-card/product-card.vue -->
<template>
<view class="card" @click="$emit('tap', product)">
<text class="name">{{ product.name }}</text>
<text class="price">¥{{ product.price }}</text>
</view>
</template>
<script>
export default {
// easycom 规则下无需手动注册
props: {
product: { type: Object, required: true }
}
}
</script>
<!-- 页面使用 -->
<template>
<view>
<product-card v-for="p in list" :key="p.id"
:product="p" @tap="goDetail" />
</view>
</template>
📚 HTML 标签速查
常用条目速查,完整版见对应教程章节。可复制代码到在线运行中测试。
| 写法 / 语法 | 作用 |
|---|---|
<h1>-<h6> | 标题,h1 最高,每页只用一个 h1 |
<p> | 段落,自动加前后间距 |
<a href=""> | 链接,href 是目标地址 |
<img src="" alt=""> | 图片,src 地址,alt 替代文字 |
<ul> / <li> | 无序列表 |
<ol> / <li> | 有序列表,数字编号 |
<dl> / <dt> / <dd> | 自定义列表:术语 + 说明 |
<table> / <tr> / <td> / <th> | 表格:行 / 单元格 / 表头 |
<form action="" method=""> | 表单,action 提交地址,method 提交方式 |
<input type=""> | 输入控件,type 有 text/password/email/number 等 |
<textarea> | 多行文本输入 |
<select> / <option> | 下拉框及选项 |
<button type="submit"> | 按钮,submit 提交表单 |
<strong> / <em> | 加粗(重要)/ 斜体(强调) |
<div> / <span> | 区块容器 / 行内容器 |
<header> <nav> <main> | 语义化:页眉 / 导航 / 主体 |
<section> <article> <aside> | 语义化:分节 / 独立内容 / 侧栏 |
<footer> | 页脚:版权、备案 |
<video> / <audio> | HTML5 视频 / 音频,加 controls 显示控制条 |
<iframe src=""> | 嵌入其他网页 |
<!-- 注释 --> | HTML 注释,不显示在页面 |
<br> / <hr> | 换行 / 水平分割线 |
<pre> | 保留空格和换行,原样显示 |
<blockquote> / <q> | 块级引用 / 行内引用 |
<figure> / <figcaption> | 图文组合及说明 |