Vue 2 中组件详解

devtools/2024/9/22 15:48:51/
  1. 什么是组件?
    在Vue中,组件是可复用的Vue实例,每个组件都可以拥有自己的模板、脚本和样式。通过组件化,我们可以将页面拆分为多个独立的、可复用的部分,使得代码更易于维护和扩展。

  2. 创建组件
    在Vue 2中,我们可以使用Vue.component()方法来创建全局组件,也可以使用Vue实例中的components选项来注册局部组件。例如:

javascript">// 全局组件
Vue.component('my-component', {// 组件选项
});// 局部组件
new Vue({el: '#app',components: {'my-component': {// 组件选项}}
});
  1. 组件通信
    1.Props(属性):
    Props 是父组件向子组件传递数据的一种方式。父组件可以通过在子组件上使用属性的方式传递数据,子组件可以在props选项中声明接收这些数据。例如:
javascript"><!-- 父组件 -->
<template><ChildComponent message="Hello from parent"></ChildComponent>
</template><!-- 子组件 -->
<script>
export default {props: ['message'],mounted() {console.log(this.message);}
}
</script>

2.emit(事件)∗∗:emit是子组件向父组件传递消息的一种方式。子组件可以通过$emit方法触发一个自定义事件,并且可以携带数据。父组件可以监听这个事件,并且接收携带的数据。例如:

javascript">
<!-- 子组件 -->
<template><button @click="sendMessage">Send Message</button>
</template><script>
export default {methods: {sendMessage() {this.$emit('message', 'Hello from child');}}
}
</script><!-- 父组件 -->
<template><ChildComponent @message="handleMessage"></ChildComponent>
</template><script>
export default {methods: {handleMessage(message) {console.log(message); // 输出:Hello from child}}
}
</script>

3.事件总线:
事件总线是一种全局通信的方式,可以用于非父子组件之间的通信。通过创建一个Vue实例作为事件总线,其他组件都可以通过这个实例来触发和监听事件。例如:

javascript">// EventBus.js
import Vue from 'vue';
export const EventBus = new Vue();// 子组件1
import { EventBus } from './EventBus';
export default {methods: {sendMessage() {EventBus.$emit('message', 'Hello from component 1');}}
}// 子组件2
import { EventBus } from './EventBus';
export default {mounted() {EventBus.$on('message', message => {console.log(message); // 输出:Hello from component 1});}
}
  1. 动态组件
    使用动态组件标签:你可以在模板中使用标签,并通过一个特定的属性(例如is)来指定要渲染的组件。这个属性的值可以是一个组件的名称或者一个组件的引用。
javascript"><template><component :is="currentComponent"></component>
</template><script>
import ComponentA from './ComponentA.vue';
import ComponentB from './ComponentB.vue';export default {data() {return {currentComponent: 'ComponentA'};},methods: {toggleComponent() {this.currentComponent = this.currentComponent === 'ComponentA' ? 'ComponentB' : 'ComponentA';}}
}
</script>

5.单文件组件
单文件组件通常包含三个部分:

1.模板(Template):包含组件的HTML结构,使用Vue的模板语法来定义组件的呈现方式。

2.脚本(Script):包含组件的JavaScript逻辑,可以包括组件的数据、方法、生命周期钩子等。

3.样式(Style):包含组件的CSS样式,可以使用普通的CSS或者预处理器(如Sass、Less)来编写。

javascript"><!-- MyComponent.vue -->
<template><div><h1>{{ message }}</h1><button @click="increment">Increment</button></div>
</template><script>
export default {data() {return {message: 'Hello Vue!',count: 0};},methods: {increment() {this.count++;}}
};
</script><style scoped>
h1 {color: blue;
}
button {background-color: green;color: white;
}
</style>

http://www.ppmy.cn/devtools/31078.html

相关文章

Pytorch学习笔记——TensorBoard的初使用

1、TensorBoard介绍 TensorBoard是TensorFlow的可视化工具&#xff0c;但它也可以与PyTorch结合使用。TensorBoard提供了一个Web界面&#xff0c;可以展示你训练过程中的各种信息&#xff0c;如损失值、准确度、权重分布等&#xff0c;更好地帮助开发者理解和调试模型。 Tenso…

vue 模板字符串

1.模板字符串换行问题 white-space: pre-wrap; 2. 鼠标移入 显示提示框 点击手动隐藏 myChart.on("mouseover", function (params) {myChart.dispatchAction({type: "downplay",}); }); tooltip: {show: true, //是否显示提示框组件&#xff0c;包括…

5分钟掌握Pydantic

数据模型定义&#xff1a;使用 Python 类来定义数据模型&#xff0c;这些类可以自动将输入转换为 Python 数据类型。 from pydantic import BaseModelclass User(BaseModel):id: intname: stremail: str类型检查&#xff1a;Pydantic 强制执行类型检查&#xff0c;确保传入的数…

HTML中input输入框(详解输入框的用法)

目录 一、input介绍 1.概念 2.好处 3.用法 4.应用 二、input语法 1.文本输入框 (type"text") 2.密码输入框 (type"password") 3.数字输入框 (type"number") 4.电子邮件输入框 (type"email") 5.复选框 (type"checkbox&…

GO语言核心30讲 进阶技术 (第一部分)

原站地址&#xff1a;Go语言核心36讲_Golang_Go语言-极客时间 一、数组和切片 1. 两者最大的不同&#xff1a;数组的长度是固定的&#xff0c;而切片的长度是可变的。 2. 可以把切片看成是对数组的一层封装&#xff0c;因为每个切片的底层数据结构中&#xff0c;一定会包含一…

数据结构与算法之经典排序算法

一、简单排序 在我们的程序中&#xff0c;排序是非常常见的一种需求&#xff0c;提供一些数据元素&#xff0c;把这些数据元素按照一定的规则进行排序。比如查询一些订单按照订单的日期进行排序&#xff0c;再比如查询一些商品&#xff0c;按照商品的价格进行排序等等。所以&a…

常用设计模式

单例模式 一个类只能创建一个对象&#xff0c;即单例模式&#xff0c;该设计模式可以保证系统中该类只有⼀个实例&#xff0c;并提供⼀个访问它的全局访问点&#xff0c;该实例被所有程序模块共享。比如在某个服务器程序中&#xff0c;该服务器的配置信息存放在⼀个文件中&…

高可用系列四:loadbalancer 负载均衡

负载均衡可以单独使用&#xff0c;也常常与注册中心结合起来使用&#xff0c;其需要解决的问题是流量分发&#xff0c;这是就需要定义分发策略&#xff0c;当然也包括了故障切换的能力。 故障切换 故障切换是负载均衡的基本能力&#xff0c;和注册中心结合时比较简单&#xf…