vue組件父與子通信詳解(一)
本文實(shí)例為大家分享了vue組件父與子通信的具體代碼,供大家參考,具體內(nèi)容如下
一、組件間通信(父組件 --> 子組件)
步驟:
①父組件在調(diào)用子組件 傳值
<child-component myValue="123"> </child-component>
②在子組件中 獲取父組件傳來(lái)的值
Vue.component('child-component',{
props:['myValue'],
template:''
})
代碼1:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>父?jìng)髯?lt;/title>
<script src="js/vue.js"></script>
</head>
<body>
<div id="container">
<p>{{msg}}</p>
<parent-component></parent-component>
</div>
<script>
// 在vue中一切都是組件
//父?jìng)髯?
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>{{"父?jìng)鬟f的值:"+myValue}}</p>
</div>
`
})
new Vue({
el:"#container",
data:{
msg:"Hello VueJs"
}
})
</script>
</body>
</html>
myValue是屬性名,必須都一樣……拿data中的用v-bind:或者:
props是property屬性,是個(gè)數(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個(gè)組件,分別是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:"注冊(cè)"
}
},
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="請(qǐng)輸入用戶名"></my-input>
<br/>
<my-label labelName="密碼"></my-label>
<my-input tips="請(qǐng)輸入密碼"></my-input>
<br/>
<my-button btnName="登錄"></my-button>
<my-button btnName="注冊(cè)"></my-button>
</form>
`
})
new Vue({
el: '#container',
data: {
msg: 'Hello Vue'
}
})
</script>
</body>
</html>
要拿到data中的數(shù)據(jù)就要v-bind,否則就不用。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Vue進(jìn)行數(shù)據(jù)可視化實(shí)踐分享
在當(dāng)今的數(shù)據(jù)驅(qū)動(dòng)時(shí)代,數(shù)據(jù)可視化變得越來(lái)越重要,它能夠幫助我們更直觀地理解數(shù)據(jù),從而做出更好的決策,在這篇博客中,我們將探索如何使用 Vue 和一些常見(jiàn)的圖表庫(kù)(如 Chart.js)來(lái)制作漂亮的數(shù)據(jù)可視化效果,需要的朋友可以參考下2024-10-10
vue定義在computed的變量無(wú)法更新問(wèn)題及解決
這篇文章主要介紹了vue定義在computed的變量無(wú)法更新問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
element的el-table自定義最后一行的實(shí)現(xiàn)代碼
最后一行要顯示一些其他結(jié)果,用的是element? ui 自帶的數(shù)據(jù)總計(jì)的屬性;返回一個(gè)數(shù)組,會(huì)按下標(biāo)進(jìn)行展示,這篇文章主要介紹了element的el-table自定義最后一行的實(shí)現(xiàn)代碼,需要的朋友可以參考下2024-03-03
Vue在項(xiàng)目中如何引入本地Json數(shù)據(jù)
這篇文章主要介紹了Vue在項(xiàng)目中如何引入本地Json數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11

