The example in this article shares with you Vue's props to implement changes in parent components and sub-components for your reference. The specific content is as follows
Similar to using v-bind to bind HTML features to an expression, you can also use v-bind to bind Define dynamic Props to the parent component's data. Whenever the data of the parent component changes, it will also be transmitted to the child component:
1 2 3 4 5 | <div>
<input v-model= "parentMsg" >
<br>
<child v-bind:my-message= "parentMsg" ></child>
</div>
|
Copy after login
It is usually simpler to use the abbreviated syntax of v-bind:
Instance:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <!DOCTYPE html>
<html lang= "en" >
<head>
<script type= "text/javascript" src= "vue.js" ></script>
<meta charset= "UTF-8" >
<title>vue的props实现父组件变化子组件一起变化</title>
</head>
<body>
<div id= "app1" >
<input v-model= "messsage" > <!--input绑定实例中data中的message-->
<div >
<child v-bind:my-message= "messsage" ></child> <!--子组件绑定实例中data中的message-->
</div>
</div>
<script>
Vue.config.debug = true;
Vue.component('child',{
props: ['myMessage'],
template: '<span>{{ myMessage }}</span><br>'
});
var vm = new Vue({
el: '#app1',
data:{
messsage:'hello you are a good boy!'
}
});
</script>
</body>
</html>
|
Copy after login