当前位置: 首页 > backend >正文

Vue:组件

组件是 Vue 的核心功能之一,它将 UI 拆分为独立、可复用的代码片段。组件可以理解为自定义的 HTML 元素,它封装了特定的功能和样式。例如一个按钮组件 <my-button>,一个导航栏组件 <app-header>,一个模态框组件 <modal-dialog>

组件的基本使用

组件注册

import ComponentA from './ComponentA.vue'export default {components: {ComponentA}
}

组件模板

单文件组件结构(.vue文件):

<template><div class="example">{{ message }}</div>
</template><script>
export default {data() {return {message: '重生之我要学Vue!'}}
}
</script><style scoped>
.example {color: red;
}
</style>

组件通信

父传子 - Props

<!-- 父组件 -->
<child-component :title="parentTitle"></child-component><!-- 子组件 -->
<script>
export default {props: {title: {type: String,default: '默认标题'}}
}
</script>

子传父 - 自定义事件

<!-- 子组件 -->
<button @click="$emit('update', newValue)">提交</button><!-- 父组件 -->
<child-component @update="handleUpdate"></child-component>

每个组件都有以下关键生命周期阶段:

  1. 创建阶段beforeCreatecreatedbeforeMountmounted
  2. 更新阶段beforeUpdateupdated
  3. 销毁阶段beforeUnmountunmounted

特殊组件类型

动态组件

<component :is="currentComponent"></component>

异步组件

const AsyncComponent = defineAsyncComponent(() =>import('./components/AsyncComponent.vue')
)

递归组件

// 组件内部调用自身
name: 'TreeItem',
template: `<li>{{ model.name }}<ul v-if="model.children"><tree-item v-for="child in model.children" :model="child"></tree-item></ul></li>
`
http://www.xdnf.cn/news/10733.html

相关文章:

  • 分班 - 华为OD统一考试(JavaScript 题解)
  • 【操作系统·windows快捷键指令】
  • sql注入补充——get注入
  • 322. 零钱兑换
  • Day10
  • 【C盘瘦身】给DevEco Studio中HarmonyOSEmulator(鸿蒙模拟器)换个地方,一键移动给C盘瘦身
  • 企业级开发中的 maven-mvnd 应用实践
  • 深度理解与剖析:Odoo系统邮箱配置指南
  • 给stm32cubeide编译出来的bin文件追加crc32
  • 算法训练第六天
  • 检索器组件深入学习与使用技巧 BaseRetriever 检索器基类
  • SystemVerilog—Interface在class中的使用
  • 【DSP数字信号处理】期末复习笔记(一)
  • 交换机、路由器配置
  • Jackson 数值转科学计数法问题分析与解决方案
  • 第一篇:揭示模型上下文协议(MCP):AI的通用连接器
  • MySQL日志
  • kafka幂等生产者和事务生产者区别
  • RK3568+LINUX + CODESYS带授权+实时系统,同时开自己的视觉应用
  • 【算法】分支限界
  • [MySQL初阶]MySQL(7) 表的内外连接
  • CQF预备知识:二、线性代数 -- 2.2.1 矩阵加法详解
  • UE5 2D地图曝光太亮怎么修改
  • MATLAB 安装与使用详细教程
  • 道路目标检测和分类数据集
  • MySQL问题:count(*)与count(1)有什么区别
  • Promise与Async/Await:现代JavaScript异步编程的利器
  • leetcode hot100 二叉树(二)
  • 项目采购管理习题剖析
  • SystemVerilog—Interface语法(一)