父子组件通信
绝大部分
vue
本身提供的通信方式,都是父子组件通信
prop
最常见的组件通信方式之一,由父组件传递到子组件
event
最常见的组件通信方式之一,当子组件发生了某些事,可以通过event
通知父组件
style
和class
父组件可以向子组件传递style
和class
,它们会合并到子组件的根元素中
示例
父组件
<template>
<div id="app">
<HelloWorld
style="color:red"
class="hello"
msg="Welcome to Your Vue.js App"
/>
</div>
</template>
<script>
import HelloWorld from "./components/HelloWorld.vue";
export default {
components: {
HelloWorld,
},
};
</script>
子组件
<template>
<div class="world" style="text-align:center">
<h1>{
{msg}}</h1>
</div>
</template>
<script>
export default {
name: "HelloWorld",
props: {
msg: String,
},
};
</script>
渲染结果:
<div id="app">
<div class="hello world" style="color:red; text-aling:center">
<h1>Welcome to Your Vue.js App</h1>
</div>
</div>
attribute
如果父组件传递了一些属性到子组件,但子组件并没有声明这些属性,则它们称之为attribute
,这些属性会直接附着在子组件的根元素上
不包括
style
和class
,它们会被特殊处理
示例
父组件
<