vue組件父與子通信詳解(一)
更新時間:2017年11月07日 11:30:21 作者:匿名的girl
這篇文章主要為大家詳細(xì)介紹了vue組件父與子通信詳解,實現(xiàn)登錄窗口,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue組件父與子通信的具體代碼,供大家參考,具體內(nèi)容如下
一、組件間通信(父組件 --> 子組件)
步驟:
①父組件在調(diào)用子組件 傳值
<child-component myValue="123"> </child-component>
②在子組件中 獲取父組件傳來的值
Vue.component('child-component',{
props:['myValue'],
template:''
})
代碼1:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>父傳子</title>
<script src="js/vue.js"></script>
</head>
<body>
<div id="container">
<p>{{msg}}</p>
<parent-component></parent-component>
</div>
<script>
// 在vue中一切都是組件
//父傳子
Vue.component("parent-component",{
data:function(){
return {
gift:"傳家寶"
}
},
template:`
<div>
<h1>這是父組件</h1>
<hr>
<child-component v-bind:myValue="gift"></child-component>
</div>
`
})
Vue.component("child-component",{
props:["myValue"],
template:`
<div>
<h1>這是子組件</h1>
<p>{{"父傳遞的值:"+myValue}}</p>
</div>
`
})
new Vue({
el:"#container",
data:{
msg:"Hello VueJs"
}
})
</script>
</body>
</html>
myValue是屬性名,必須都一樣……拿data中的用v-bind:或者:
props是property屬性,是個數(shù)組
代碼2:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>父子之間通信練習(xí)</title>
<script src="js/vue.js"></script>
</head>
<body>
<div id="container">
<p>{{msg}}</p>
<my-login></my-login>
</div>
<script>
/*
登錄窗口
創(chuàng)建4個組件,分別是my-label my-input my-button my-login(復(fù)合組件)
*/
Vue.component("my-label",{
props:["myLabel"],
template:`
<div>
<label>{{myLabel}}</label>
</div>
`
})
Vue.component("my-input",{
template:`
<div>
<input type="text"/>
</div>
`
})
Vue.component("my-button",{
props:["myButton"],
template:`
<div>
<button>{{myButton}}</button>
</div>
`
})
//復(fù)合組件
Vue.component("my-login",{
data:function(){
return {
uname:"用戶名",
upwd:"密碼",
login:"登錄",
register:"注冊"
}
},
template:`
<div>
<my-label v-bind:myLabel="uname"></my-label>
<my-input></my-input>
<my-label v-bind:myLabel="upwd"></my-label>
<my-input></my-input>
<my-button v-bind:myButton="login"></my-button>
<my-button v-bind:myButton="register"></my-button>
</div>
`
})
new Vue({
el:"#container",
data:{
msg:"Hello VueJs"
}
})
</script>
</body>
</html>
代碼3:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<script src="js/vue.js"></script>
<title></title>
</head>
<body>
<div id="container">
<my-login></my-login>
</div>
<script>
Vue.component('my-label',{
props:['labelName'],
template:'<label>{{labelName}}</label>'
})
Vue.component('my-input',{
props:['tips'],
template:'<input type="text" :placeholder="tips"/>'
})
Vue.component('my-button',{
props:['btnName'],
template:'<button>{{btnName}}</button>'
})
Vue.component('my-login',{
template:`
<form>
<my-label labelName="用戶名"></my-label>
<my-input tips="請輸入用戶名"></my-input>
<br/>
<my-label labelName="密碼"></my-label>
<my-input tips="請輸入密碼"></my-input>
<br/>
<my-button btnName="登錄"></my-button>
<my-button btnName="注冊"></my-button>
</form>
`
})
new Vue({
el: '#container',
data: {
msg: 'Hello Vue'
}
})
</script>
</body>
</html>
要拿到data中的數(shù)據(jù)就要v-bind,否則就不用。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Vue進(jìn)行數(shù)據(jù)可視化實踐分享
在當(dāng)今的數(shù)據(jù)驅(qū)動時代,數(shù)據(jù)可視化變得越來越重要,它能夠幫助我們更直觀地理解數(shù)據(jù),從而做出更好的決策,在這篇博客中,我們將探索如何使用 Vue 和一些常見的圖表庫(如 Chart.js)來制作漂亮的數(shù)據(jù)可視化效果,需要的朋友可以參考下2024-10-10
element的el-table自定義最后一行的實現(xiàn)代碼
最后一行要顯示一些其他結(jié)果,用的是element? ui 自帶的數(shù)據(jù)總計的屬性;返回一個數(shù)組,會按下標(biāo)進(jìn)行展示,這篇文章主要介紹了element的el-table自定義最后一行的實現(xiàn)代碼,需要的朋友可以參考下2024-03-03

