vue2 父子组件传值 举个例子
时间: 2023-08-02 12:33:57 浏览: 57
好的,假设有一个父组件 Parent 和一个子组件 Child,现在需要在 Parent 中传递一个值给 Child,具体步骤如下:
1. 在 Parent 中定义要传递的值,比如:
```
<template>
<div>
<child :msg="message"></child>
</div>
</template>
<script>
import Child from './Child.vue';
export default {
components: {
Child
},
data() {
return {
message: 'Hello World!'
}
}
}
</script>
```
2. 在 Child 中通过 props 接收传递过来的值,比如:
```
<template>
<div>{{ msg }}</div>
</template>
<script>
export default {
props: {
msg: String
}
}
</script>
```
这样就完成了父子组件之间的传值,父组件通过在子组件中定义 props,然后把要传递的值绑定到子组件的 props 上实现传值。
相关问题
写一个vue父子组件传值的例子
父组件:
```vue
<template>
<div>
<p>父组件的count值:{{ count }}</p>
<button @click="increment">增加</button>
<Child :child-count="count" @child-increment="increment"></Child>
</div>
</template>
<script>
import Child from './Child.vue'
export default {
components: {
Child
},
data() {
return {
count: 0
}
},
methods: {
increment() {
this.count++
}
}
}
</script>
```
子组件:
```vue
<template>
<div>
<p>子组件的count值:{{ childCount }}</p>
<button @click="childIncrement">增加</button>
</div>
</template>
<script>
export default {
props: {
childCount: {
type: Number,
required: true
}
},
methods: {
childIncrement() {
this.$emit('child-increment')
}
}
}
</script>
```
在父组件中,通过在子组件上绑定`child-count`属性,将父组件的`count`值传递给子组件。同时,通过在子组件上绑定`@child-increment`事件,监听子组件的增加操作,然后调用父组件的`increment`方法,实现父子组件之间的通信。
vue3.0父子组件传值
在Vue 3.0中,父子组件之间的传值可以使用props和emit实现。props是父组件向子组件传递数据的一种方式,子组件通过声明props来接收父组件传递的值。父组件可以将数据通过属性的形式绑定到子组件上,子组件可以在props属性中声明接收该属性的类型和默认值。例如:
父组件代码:
<template>
<div>
<child-component :message="parentMessage"></child-component>
</div>
</template>
<script>
import ChildComponent from "./ChildComponent.vue";
export default {
components: {
ChildComponent,
},
data() {
return {
parentMessage: "Hello from parent",
};
},
};
</script>
子组件代码:
<template>
<div>
<p>{{ message }}</p>
</div>
</template>
<script>
export default {
props: {
message: {
type: String,
default: "",
},
},
};
</script>
在这个例子中,父组件传递了一个名为parentMessage的数据到子组件中,子组件使用props接收到父组件传递的message,并在模板中显示出来。
除了props,Vue 3.0还引入了新的API emit,它可以用于在子组件中向父组件发送消息。子组件可以通过$emit方法发送一个自定义事件,并可以传递需要发送的数据。父组件可以通过在子组件上监听该事件,并在回调中接收子组件发送的数据。例如:
子组件代码:
<template>
<div>
<button @click="sendMessage">Send Message</button>
</div>
</template>
<script>
export default {
methods: {
sendMessage() {
this.$emit("messageSent", "Hello from child");
},
},
};
</script>
父组件代码:
<template>
<div>
<child-component @messageSent="handleMessage"></child-component>
</div>
</template>
<script>
import ChildComponent from "./ChildComponent.vue";
export default {
components: {
ChildComponent,
},
methods: {
handleMessage(message) {
console.log(message);
},
},
};
</script>
在这个例子中,子组件中的按钮点击事件调用sendMessage方法,该方法使用$emit发送一个名为messageSent的自定义事件,并将"Hello from child"作为参数传递。父组件在模板中使用@messageSent监听该事件,并通过handleMessage方法处理接收到的消息。在handleMessage方法中,我们打印出了从子组件发送的消息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)