父子组件通信-props属性

父组件通过props向下传递数据给子组件;

组件实例的作用域是孤立的,子组件用props选项声明他期望得到的数据。

字面量语法:值是字符串,显示出来是1+1,如果想要传递number数值,在message前面加上v-bind:

<child message="1+1"></child>

<div id="app">
	<child message="1+1"></child>
</div>
	
<script type="text/javascript">
	//创建子组件
	var componentChild = Vue.component('child',{
		props:['message'],
		template:'<p>{{message}}</p>'
	})
	var vm = new Vue({
		el:'#app',
		components:{
			componentChild
		}
	})
</script>

单向数据流:prop是单向绑定,父组件更新,会传递给子组件。




动态prop:(在属性前面加上:)

命名方式:父组件实例使用子组件时,使用短横线(-);子组件中props的选项,使用驼峰式

<div id="app1">
	<input type="text" v-model="msg"/><br />
	<child1 :my-message="msg"></child1>
</div>
<script>
	Vue.component('child1',{
		props:['myMessage'],
		template:'<p>{{myMessage}}</p>'
	})
	var vm1 = new Vue({
		el:'#app1',
		data:function(){
			return{
				msg:'zhaoxingpei'
			}
		}
	})
</script>


猜你喜欢

转载自blog.csdn.net/zx_p24/article/details/74285055