Vue組件間的通信--父傳子
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="app">
<parent></parent>
</div>
<script src="js/vue.min.js"></script>
<script>
//1:創建父組件
Vue.component("parent",{
data:function(){
return {money:3000}
},
template:`
<div>
<h4>父組件</h4>
<child :myValue="money"></child>
</div>
`
});
//2:創建子組件
Vue.component("child",{
template:`<div><h3>子組件</h3>
{{myValue}}
</div>`,
props:["myValue"], // 聲明變量保存父組件數據
mounted:function(){
//聲明變量結束,獲取父元素數據.
//己存保存 this.data
console.log(this.myValue);
}
});
//3:創建Vue
new Vue({el:"#app"});
</script>
</body>
</html>
<body>
<div id="app">
<my-login></my-login>
</div>
<script src="vue.min.js"></script>
<script>
Vue.component("my-login",{
template:`
<div>
<h3>父組件</h3>
username
<my-input tips="用戶名"></my-input>
password
<my-input tips="密碼"></my-input>
</div>
`
});
Vue.component("my-input",{
props:['tips'],
template:`
<input type="text" :placeholder="tips" />
`
});
new Vue({el:"#app"});
</script>
</body>
Vue組件間的通信--父傳子