Vue.js組件使用props傳遞數(shù)據(jù)的方法
本文實例為大家分享了Vue.js使用props傳遞數(shù)據(jù)的具體代碼,供大家參考,具體內(nèi)容如下
基本用法
通常父組件的模板中包含子組件,父組件要正向地向子組件傳遞數(shù)據(jù)或參數(shù),子組件接收到后根據(jù)參數(shù)的不同來渲染不同的內(nèi)容或執(zhí)行操作。這個正向傳遞數(shù)據(jù)的過程就是通過props來實現(xiàn)的。
在組件中,使用選項props來聲明需要從父級接收的數(shù)據(jù),props的值可以是兩種,一種是字符串數(shù)組,一種是對象。
示例:構(gòu)造一個數(shù)組,接收一個來自父組件的message,并把它再組件模板中渲染
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<title>props</title>
</head>
<body>
<div id="myApp">
<my-component message="來自父組件的數(shù)據(jù)"></my-component>
</div>
<script>
Vue.component('my-component',{
props: ['message'],
template: '<div>{{message}}</div>'
});
var myApp = new Vue({
el: '#myApp'
});
</script>
</body>
</html>
props中聲明的數(shù)據(jù)與組件函數(shù)return的數(shù)據(jù)主要區(qū)別是:**props的數(shù)據(jù)來自父級,而data中的數(shù)據(jù)是組件自己的數(shù)據(jù),作用域是組件本身。**這兩種數(shù)據(jù)都可以在模板template及計算屬性computed和方法methods中使用。
上例的數(shù)據(jù)message就是通過props從父級傳遞過來的,在組件的字的那個一標簽上直接寫該props的名稱,如果要傳遞多個數(shù)據(jù),在props數(shù)組中添加項即可。
注意:由于HTML特性不區(qū)分大小寫,當使用DOM模板時,駝峰命名的props名稱要轉(zhuǎn)為短橫分割命名,例如:
<div id="app">
<my-component warning-text="提示信息"></my-component>
</div>
<script>
//如果使用字符串模板,可以忽略這些限制
Vue.component('my-component',{
props: ['warningText'],
template: '<div>{{warningText}}</div>'
});
var app = new Vue({
el: '#app'
});
</script>
有時候,傳遞的數(shù)據(jù)并不是直接寫死的,而是來自父級的動態(tài)數(shù)據(jù),這時候可以使用指令v-bing來動態(tài)綁定props的值,當父組件的數(shù)據(jù)變化時,也會傳遞給子組件。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<title>動態(tài)綁定</title>
</head>
<body>
<div id="app">
<input type="text" v-model="parentMessage">
<my-component :message="parentMessage"></my-component>
</div>
<script>
Vue.component('my-component',{
props: ['message'],
template: '<div>{{message}}</div>'
});
var app = new Vue({
el: '#app',
data: {
parentMessage: ''
}
});
</script>
</body>
</html>

上例使用v-model綁定了父級的數(shù)據(jù)parentMessage,當通過輸入框任意輸入時,子組件接受到的props "message"也會實時響應,并更新組件模板。
更多教程點擊《Vue.js前端組件學習教程》,歡迎大家學習閱讀。
關(guān)于vue.js組件的教程,請大家點擊專題vue.js組件學習教程進行學習。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
VUE識別訪問設(shè)備是pc端還是移動端的實現(xiàn)步驟
經(jīng)常在項目中會有支持pc與手機端需求,并且pc與手機端是兩個不一樣的頁面,這時就要求判斷設(shè)置,下面這篇文章主要給大家介紹了關(guān)于VUE識別訪問設(shè)備是pc端還是移動端的相關(guān)資料,需要的朋友可以參考下2023-05-05
Vue如何實現(xiàn)數(shù)據(jù)的上移和下移
這篇文章主要介紹了Vue如何實現(xiàn)數(shù)據(jù)的上移和下移問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
webpack 3 + Vue2 使用dotenv配置多環(huán)境的步驟
這篇文章主要介紹了webpack 3 + Vue2 使用dotenv配置多環(huán)境,env文件在配置文件都可以用, vue頁面用的時候需要在 webpack.base.conf.js 重新配置,需要的朋友可以參考下2023-11-11
關(guān)于vue3?解決getCurrentInstance?打包后線上環(huán)境報錯問題
這篇文章主要介紹了vue3?解決getCurrentInstance?打包后線上環(huán)境報錯問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05

