詳解Vue中的Props與Data細微差別
Vue提供了兩種不同的存儲變量:props和data。
這些方法一開始可能會讓人感到困惑,因為它們做的事情很相似,而且也不清楚什何時使用props,何時使用data。
那么props和data有什么區(qū)別呢?
data是每個組件的私有內(nèi)存,可以在其中存儲需要的任何變量。props是將數(shù)據(jù)從父組件傳遞到子組件的方式。
在本文中,我們將學(xué)習(xí)到:
- 什么是props,為什么這些數(shù)據(jù)只向下流動,而不是向上
- data 選項的用途
- 響應(yīng)式是什么
- 如何避免 props 和 data 之間的命名沖突
- 如何將 props 和 data 結(jié)合使用
什么是 props
在Vue中,props(或properties)是我們將數(shù)據(jù)從父組件向下傳遞到其子組件的方式。
當(dāng)我們使用組件構(gòu)建應(yīng)用程序時,最終會構(gòu)建一個稱為樹的數(shù)據(jù)結(jié)構(gòu)。 類似于家譜,具有:
- 父母
- 孩子
- 祖先
- 子孫
數(shù)據(jù)從根組件(位于最頂端的組件)沿著樹向下流動。就像基因是如何代代相傳的一樣,父組件也會將自己的props傳給了他們的孩子。
在Vue中,我們在代碼的<template>中向組件添加了一些props
<template> <my-component cool-prop="hello world"></my-component> </template>
在這個例子中,我們傳遞一個名為color-prop prop,其值為“hello world”。我們能夠從my-component內(nèi)部訪問這個值。
然而,當(dāng)我們從組件內(nèi)部訪問props時,我們并不擁有它們,所以我們不能更改它們(就像你不能改變你父母給你的基因一樣)。
注意:雖然可以更改組件中的屬性,但這是一個非常糟糕的主意。最終還會更改父類正在使用的值,這可能會導(dǎo)致很多混淆。
但是有些情況我們需要改變變量,所以 data 就派上用場了。
什么是 data ?
data是每個組件的內(nèi)存,這是存儲數(shù)據(jù)和希望跟蹤的任何其他變量的地方。
如果我們正在構(gòu)建一個計數(shù)器應(yīng)用程序,我們將需要跟蹤計數(shù),因此我們將向我們的data添加一個count:
<template>
<div>
{{ count }}
<button @click="increment">+</button>
<button @click="decrement">-</button>
</div>
</template>
------------------------------------------
export default {
name: 'Counter',
data() {
return {
// Initialized to zero to begin
count: 0,
}
},
methods: {
increment() {
this.count += 1;
},
decrement() {
this.count -= 1;
}
}
}
此處的data是私有的,僅供組件本身使用,其他組件不能訪問它。
注意:理論上是其它組件是不能訪問這些數(shù)據(jù),但實際是可以的。但是出于同樣的原因,這樣做是非常糟糕的
如果需要向組件傳遞數(shù)據(jù),可以使用props向下傳遞數(shù)據(jù)(傳遞給子組件),或者使用事件向上傳遞數(shù)據(jù)(傳遞給父組件)。
props 和 data 都是響應(yīng)式的
使用 Vue,我們不需要過多地考慮組件什么時候會更新,Vue 會自動幫我們做好,因為 Vue 是響應(yīng)式的。
我們不必每次更改 data 都調(diào)用setState,只需更改data即可! 只要要更新具有響應(yīng)式的屬性(props,computed 及 data 中的任何值),Vue 就會知道它何時發(fā)生變化。
回到計數(shù)器應(yīng)用程序,我們仔細看看這里面的方法
methods: {
increment() {
this.count += 1;
},
decrement() {
this.count -= 1;
}
}
我們所要做的就是更新count,Vue 會檢測到這個變化,然后用新值重新渲染我們的應(yīng)用程序
Vue 的響應(yīng)系統(tǒng)有很多細微的差別,如果你想要高效地使用Vue,理解它是非常重要的。如果你想更深入地了解Vue的響應(yīng)系統(tǒng),這里有更多要了解的東西。
避免命名沖突
Vue所做的另一件事是,使開發(fā)工作變得更好一點。
我們在組件上定義一些 props 和 data
export default {
props: ['propA', 'propB'],
data() {
return {
dataA: 'hello',
dataB: 'world',
};
},
};
如果要在方法內(nèi)部訪問它們,則不必使用this.props.propA或this.data.dataA。 Vue 讓我們完全省略了 props 和 dasta,只剩下了更整潔的代碼。
我們可以使用this.propA或this.dataA訪問它們:
methods: {
coolMethod() {
// Access a prop
console.log(this.propA);
// Access our data
console.log(this.dataA);
}
}
因此,如果我們不小心在data和 props中使用相同的名稱,則會遇到問題。
如果發(fā)生這種情況,Vue 會給你一個警告,因為它不知道你想訪問哪個。
export default {
props: ['secret'],
data() {
return {
secret: '1234',
};
},
methods: {
printSecret() {
// 我們想要哪一個?
console.log(this.secret);
}
}
};
當(dāng)我們同時使用props和data時,Vue 的神奇之處就產(chǎn)生了。
props 和 data 一起使用
既然我們已經(jīng)看到了 props 和 data 的不同之處,讓我們來看看為什么我們需要兩個,通過建立一個基本的應(yīng)用程序。
我們將使用名為ContactInfo的組件顯示此信息:
// ContactInfo
<template>
<div class="container">
<div class="row">
Email: {{ emailAddress }}
Twitter: {{ twitterHandle }}
Instagram: {{ instagram }}
</div>
</div>
</template>
-------------------------------------
export default {
name: 'ContactInfo',
props: ['emailAddress', 'twitterHandle', 'instagram'],
};
ContactInfo組件接受 props:emailAddress,twitterHandle和instagram,并將其顯示在頁面上。
我們的個人資料頁面組件ProfilePage如下所示:
// ProfilePage
<template>
<div class="profile-page">
<div class="avatar">
<img src="user.profilePicture" />
{{ user.name }}
</div>
</div>
</template>
-------------------------------------------------
export default {
name: 'ProfilePage',
data() {
return {
// In a real app we would get this data from a server
user: {
name: 'John Smith',
profilePicture: './profile-pic.jpg',
emailAddress: 'john@smith.com',
twitterHandle: 'johnsmith',
instagram: 'johnsmith345',
},
}
}
};
我們的ProfilePage組件目前顯示用戶的配置文件圖片及其名稱,它還有用戶數(shù)據(jù)對象。
我們?nèi)绾螐母附M件(ProfilePage)向下獲取數(shù)據(jù)到子組件(ContactInfo)
我們必須使用 props 傳遞數(shù)據(jù)。
首先,我們需要將ContactInfo組件導(dǎo)入ProfilePage組件
// Import the component
import ContactInfo from './ContactInfo.vue';
export default {
name: 'ProfilePage',
// Add it as a dependency
components: {
ContactInfo,
},
data() {
return {
user: {
name: 'John Smith',
profilePicture: './profile-pic.jpg',
emailAddress: 'john@smith.com',
twitterHandle: 'johnsmith',
instagram: 'johnsmith345',
},
}
}
};
其次,我們必須在<template>中添加組件:
// ProfilePage
<template>
<div class="profile-page">
<div class="avatar">
<img src="user.profilePicture" />
{{ user.name }}
</div>
<!-- Add component in with props -->
<contact-info
:email-address="emailAddress"
:twitter-handle="twitterHandle"
:instagram="instagram"
/>
</div>
</template>
現(xiàn)在ContactInfo需要的所有用戶數(shù)據(jù)都將沿著組件樹向下流動,并從ProfilePage進入ContactInfo。
我們將數(shù)據(jù)保存在ProfilePage而不是ContactInfo中的原因是ProfilePage頁面的其他部分需要訪問user對象。
由于數(shù)據(jù)只向下流,這意味著我們必須將數(shù)據(jù)放在組件樹中足夠高的位置,以便它可以向下流到需要去的所有位置。
原文:https://medium.com/js-dojo/vue-data-flow-how-it-works-3ff316a7ffcd
PS:vue中把props中的值賦值給data
父組件:
<template>
<div>
<navbar :ctype="ctype"></navbar>
</div>
</template>
<script>
import navbar from '@/components/navbar'
export default {
components: {navbar},
data () {
return{
ctype:1
}
}
}
</script>
子組件:
<template>
<div>
<div>{{thistype}}</div>
</div>
</template>
<script>
export default {
props:['ctype'],
computed: {
normalizedSize: function () {
return this.ctype.trim().toLowerCase()
}
},
data(){
return{
thistype:this.ctype
}
}
}
</script>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue 頁面狀態(tài)保持頁面間數(shù)據(jù)傳輸?shù)囊环N方法(推薦)
vue router給我們提供了兩種頁面間傳遞參數(shù)的方式,一種是動態(tài)路由匹配,一種是編程式導(dǎo)航,接下來通過本文給大家介紹Vue 頁面狀態(tài)保持頁面間數(shù)據(jù)傳輸?shù)囊环N方法,需要的朋友可以參考下2018-11-11
Vue3中watch監(jiān)聽對象的屬性值(監(jiān)聽源必須是一個getter函數(shù))
這篇文章主要介紹了Vue3中watch監(jiān)聽對象的屬性值,監(jiān)聽源必須是一個getter函數(shù),本文結(jié)合示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
VUE識別訪問設(shè)備是pc端還是移動端的實現(xiàn)步驟
經(jīng)常在項目中會有支持pc與手機端需求,并且pc與手機端是兩個不一樣的頁面,這時就要求判斷設(shè)置,下面這篇文章主要給大家介紹了關(guān)于VUE識別訪問設(shè)備是pc端還是移動端的相關(guān)資料,需要的朋友可以參考下2023-05-05
Vue引入并使用Element組件庫的兩種方式小結(jié)
本文主要介紹了Vue引入并使用Element組件庫的兩種方式小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

