Vue組件之單向數(shù)據(jù)流的解決方法
子組件能夠通過自身的props選項(xiàng)獲取父組件上的數(shù)據(jù),但是在默認(rèn)情況下,props是單向綁定的---當(dāng)父組件數(shù)據(jù)(屬性)發(fā)生變化的時(shí)候會傳遞給子組件,引起子組件的變化,但不能反過來并且不允許子組件直接改變父組件的數(shù)據(jù),會報(bào)錯(cuò)的。例如:
也就是說當(dāng)通過一種方法改變父組件數(shù)據(jù)的時(shí)候,子組件與之相關(guān)聯(lián)的props數(shù)據(jù)也會發(fā)生改變,從而影響子組件,但是子組件直接改變從父組件拿過來的props數(shù)據(jù)卻不能影響父組件的原始數(shù)據(jù)。也就是說一般情況下只能是“父影響子,而不是子影響父”。
兩種情況:
1.如果子組件想將從父組件獲得的數(shù)據(jù)作為局部數(shù)據(jù)來使用,可以將其給保存到子組件的局部變量data中(子組件中的變量),不影響父組件的數(shù)據(jù);例如:
data:function(){
return {
weather:{
tempre:"22.3℃",
weth:"rain",
wind:this.ser
}
}
},
這里的this.sers就是來源于子組件的props數(shù)據(jù)。
2.如果子組件想修改數(shù)據(jù)并且同步更新到父組件,兩種解決方式
第一種:使用.sync加上顯式觸發(fā)的一個(gè)事件this.$emit("update:你要更改的props數(shù)據(jù)", 改變后的值),也就是在一個(gè)事件觸發(fā)的函數(shù)中通過this.$emit("update:你要更改的props數(shù)據(jù)", 改變后的值)來改變數(shù)據(jù);例如:
HTML部分
<div id= "container" v-cloak>
<my-compon></my-compon>
</div>
<!-- 父組件模板 -->
<template id="myComp">
<div>
<h3>大家好,我是{{animal.name}}貓,我已經(jīng)和Jerry斗爭了{(lán){animal.age}}年了</h3>
給綁定的數(shù)據(jù)使用.sync修飾符
<my-comp-son v-bind:animalage.sync="animal.age"></my-comp-son>
</div>
</template>
<!-- 子組件模板 -->
<template id="myCompSon">
<div>
<h4>一只皮毛是{{dog.hair}}色,身高是{{dog.height}}的狗狗,在散步。。。</h4>
<h3>今天的天氣:{{weather.weth}},風(fēng)力{{weather.wind}},溫度{{weather.tempre}},{{animalname}},{{animalage}}</h3>
<button @click = "changeFatDaAge">點(diǎn)擊父組件中的數(shù)據(jù)會跟著改變方式一</button>
</div>
</template>
JS部分
var app = new Vue({
el:"#container",
data:{
house:{
date:"2017-10-10",
area:"144m²",
floor:6,
},
carBrand:"Benzi"
},
components:{
"my-compon":{//父組件
template:"#myComp",
data:function(){
return {
animal:{
name:"Tom",
age:3,
skin:"black"
},
shoe:"鴻星爾克",
dog:{
hair:"brown",
height:1.25
}
}
},
methods: {
changeData:function () {//這里的this指的是當(dāng)前父組件的實(shí)例
this.animal.name = "Kitty"http://改變父組件中的數(shù)據(jù)
}
},
components:{
"my-comp-son":{//子組件
template:"#myCompSon",
props:["animalname","animalage","dog"],//地位和data一樣,獲取方式也是一樣
data:function(){
return {
weather:{
tempre:"22.3℃",
weth:"rain",
wind:"3級"
}
}
},
methods:{
// 給v-bind使用修飾符.sync,需要顯式地觸發(fā)一個(gè)更新事件(this.$emit("update:你要更改的props數(shù)據(jù)", 改變后的值))
changeFatDaAge:function(){
// this.animalage = 19;
this.$emit("update:animalage", 19)//通過這個(gè)方法來改變子組件props數(shù)據(jù),并引起父組件相應(yīng)數(shù)據(jù)的改變
}
}
}
}
}
}
})
當(dāng)點(diǎn)擊按鈕的時(shí)候父組件上的原始數(shù)據(jù)也會發(fā)生改變,不過這種方式不常用,寫法也太麻煩,不建議使用;
第二種:將父組件的數(shù)據(jù)包裝成對象并綁定到子組件上,在子組件中修改對象的屬性(其實(shí)并沒有真正改變該對象,因?yàn)閷ο笫且妙愋偷臄?shù)據(jù),雖然屬性發(fā)生了變化,但指針并沒有發(fā)生變化),常用。例如:
HTML部分:
<div id= "container" v-cloak>
<my-compon></my-compon>
</div>
<!-- 父組件模板 -->
<template id="myComp">
<div>
<h4>一只皮毛是{{dog.hair}}色,身高是{{dog.height}}的狗狗,在散步。。。</h4>
<!-- 將父組件的數(shù)據(jù)包裝成對象并綁定到子組件上,在子組件中修改對象的屬性,在這是dog -->
<my-comp-son :dog = "dog"></my-comp-son>
</div>
</template>
<!-- 子組件模板 -->
<template id="myCompSon">
<div>
<h4>一只皮毛是{{dog.hair}}色,身高是{{dog.height}}的狗狗,在散步。。。</h4>
<button @click="changeFondata">點(diǎn)擊父組件中的數(shù)據(jù)會跟著改變方式二</button>
</div>
</template>
JS部分
var app = new Vue({
el:"#container",
data:{
house:{
date:"2017-10-10",
area:"144m²",
floor:6,
},
carBrand:"Benzi"
},
components:{
"my-compon":{//父組件
template:"#myComp",
data:function(){
return {
animal:{
name:"Tom",
age:3,
skin:"black"
},
shoe:"鴻星爾克",
dog:{
hair:"brown",
height:1.25
}
}
},
methods: {
changeData:function () {//這里的this指的是當(dāng)前父組件的實(shí)例
this.animal.name = "Kitty"http://改變父組件中的數(shù)據(jù)
}
},
components:{
"my-comp-son":{//子組件
template:"#myCompSon",
props:["animalname","animalage","dog"],//地位和data一樣,獲取方式也是一樣
data:function(){
return {
weather:{
tempre:"22.3℃",
weth:"rain",
wind:"3級"
}
}
},
methods:{
//在子組件中修改對象的屬性
changeFondata:function(){
this.dog.hair = "紅"
}
}
}
}
}
}
})
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)動(dòng)態(tài)表單動(dòng)態(tài)渲染組件的方式(1)
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)動(dòng)態(tài)表單動(dòng)態(tài)渲染組件的方式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
vue-router中關(guān)于children的使用方法
這篇文章主要介紹了vue-router中關(guān)于children的使用方法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Vuejs學(xué)習(xí)筆記之使用指令v-model完成表單的數(shù)據(jù)雙向綁定
表單類控件承載了一個(gè)網(wǎng)頁數(shù)據(jù)的錄入與交互,本章將介紹如何使用指令v-model完成表單的數(shù)據(jù)雙向綁定功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值。感興趣的朋友跟隨小編一起看看吧2019-04-04
前端實(shí)現(xiàn)pdf預(yù)覽功能的全過程(基于vue)
這篇文章主要給大家介紹了關(guān)于前端實(shí)現(xiàn)pdf預(yù)覽功能的相關(guān)資料,前端實(shí)現(xiàn)預(yù)覽最好的效果還是PDF,不會出現(xiàn)一些文字錯(cuò)亂和亂碼的問題,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09
vue2.0 父組件給子組件傳遞數(shù)據(jù)的方法
在父組件 App.vue 中引用子組件 A.vue,把 name 的值傳給 A 組件。這篇文章主要介紹了vue2.0 父組件給子組件傳遞數(shù)據(jù)的方法,需要的朋友可以參考下2018-01-01
vue自定義指令的創(chuàng)建和使用方法實(shí)例分析
這篇文章主要介紹了vue自定義指令的創(chuàng)建和使用方法,結(jié)合完整實(shí)例形式分析了vue.js創(chuàng)建及使用自定義指令的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-12-12
Vue使用axios post方式將表單中的數(shù)據(jù)以json格式提交給后端接收操作實(shí)例
這篇文章主要介紹了Vue使用axios post方式將表單中的數(shù)據(jù)以json格式提交給后端接收操作,結(jié)合實(shí)例形式分析了vue基于axios庫post傳送表單json格式數(shù)據(jù)相關(guān)操作實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2023-06-06

