- 先在父组件中的子组件标签中绑定值
<Child :data1='msg'/>
- 在子组件中使用props接受参数列表
props:["data1"]
- 然后就可以在子组件中任意使用了
看不太懂?算了,直接看代码吧
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>vue.js</title>
</head>
<body>
<div id="app">
</div>
</body>
<script type="text/javascript" src="https://cdn.bootcss.com/vue/2.5.10/vue.min.js"></script>
<script type="text/javascript">
var Child={
template:'<h1>我是子组件 <span :style="childData.style">{{childData.msg1}}</span> </h1>',
props:["childData"]
};
var APP={
template:"<div> <Child :childData='msg' /> </div>"
,
data()
{
return{
msg:{
msg1:"消息1",
style:"color:red",
}
}
}
,
components:{
Child
}
};
new Vue({
el: "#app"
,
template:'<APP/>'
,
components:{
APP,
},
methods:{
}
})
</script>
</html>