vue組件Prop傳遞數(shù)據(jù)的實(shí)現(xiàn)示例
組件實(shí)例的作用域是孤立的。這意味著不能(也不應(yīng)該)在子組件的模板內(nèi)直接引用父組件的數(shù)據(jù)。要讓子組件使用父組件的數(shù)據(jù),我們需要通過子組件的props選項(xiàng)。
prop 是單向綁定的:當(dāng)父組件的屬性變化時(shí),將傳導(dǎo)給子組件,但是不會(huì)反過來。這是為了防止子組件無意修改了父組件的狀態(tài)。
每次父組件更新時(shí),子組件的所有 prop 都會(huì)更新為最新值。這意味著你不應(yīng)該在子組件內(nèi)部改變 prop。
1、Prop靜態(tài)傳遞數(shù)據(jù)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="vue.js"></script> </head> <body > <div id="app"> <!--靜態(tài)傳遞數(shù)據(jù)--> <my-component message="hello" name="劉二狗" age="18"></my-component> </div> </body> <script> Vue.component('my-component',{ //子組件使用父組件的數(shù)據(jù) message name age props:['message','name','age'], //用data選項(xiàng)對(duì)數(shù)據(jù)進(jìn)行處理 data:function(){ return{ message1: this.message +'用data選項(xiàng)對(duì)數(shù)據(jù)進(jìn)行處理' } }, //用計(jì)算屬性選項(xiàng)對(duì)數(shù)據(jù)進(jìn)行處理 computed:{ message2:function(){ return this.message + '用計(jì)算屬性選項(xiàng)對(duì)數(shù)據(jù)進(jìn)行處理' } }, template:'<div>' + '<span>{{message1}}</span><br>'+ '<span>{{message2}}</span><br>'+ '<span>{{message}} {{name}}今年{{age}}了</span><br>'+ '</div>' }) new Vue({ el:'#app' }) </script> </html>
輸出結(jié)果:
2、Prop動(dòng)態(tài)傳遞數(shù)據(jù)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="vue.js"></script> </head> <body > <div id="app"> <input v-model="parentMsg"><br> <my-component :message="parentMsg"></my-component> </div> </body> <script> Vue.component('my-component',{ props:['message'], data:function(){ return{count:this.message+'劉三狗的嫉妒了'} }, computed:{ normalizedSize: function () { return this.message.trim().toLowerCase() } }, template:'<div>' + '<span>{{message}}---{{normalizedSize}}</span><br>'+ '<span>{{count}}</span>'+ '</div>' }) new Vue({ el:'#app', data:{ parentMsg:'哈哈哈' } }) </script> </html>
輸出結(jié)果:
3、Prop驗(yàn)證,我們可以為組件的 props 指定驗(yàn)證規(guī)格。如果傳入的數(shù)據(jù)不符合規(guī)格,Vue 會(huì)發(fā)出警告。在前臺(tái)的控制器中可以看到警告信息。
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script type="text/javascript" src="vue.js"></script> </head> <body> <div id="app"> <example :prop-d="message"></example> </div> </body> <script> Vue.component('example', { props: { // 基礎(chǔ)類型檢測 (`null` 意思是任何類型都可以) propA: Number, // 多種類型 propB: [String, Number], // 必傳且是字符串 propC: { type: String, required: true }, // 數(shù)字,有默認(rèn)值 propD: { type: Number, default: 100 }, // 數(shù)組/對(duì)象的默認(rèn)值應(yīng)當(dāng)由一個(gè)工廠函數(shù)返回 propE: { type: Object, default: function () { return { message: 'hello' } } }, // 自定義驗(yàn)證函數(shù) propF: { validator: function (value) { return value > 10 } } }, template:'<span>{{propD}}</span>' }) new Vue({ el:'#app', data:{ message:'propD驗(yàn)證只能傳入數(shù)字類型' } }) </script> </html>
控制臺(tái)輸出的警告信息:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue父組件向子組件(props)傳遞數(shù)據(jù)的方法
- vuejs動(dòng)態(tài)組件給子組件傳遞數(shù)據(jù)的方法詳解
- vue.js組件之間傳遞數(shù)據(jù)的方法
- vue組件中使用props傳遞數(shù)據(jù)的實(shí)例詳解
- vue中各組件之間傳遞數(shù)據(jù)的方法示例
- Vue2.x中的父組件傳遞數(shù)據(jù)至子組件的方法
- vue2.0 父組件給子組件傳遞數(shù)據(jù)的方法
- vue子組件使用自定義事件向父組件傳遞數(shù)據(jù)
- vue項(xiàng)目中做編輯功能傳遞數(shù)據(jù)時(shí)遇到問題的解決方法
- Vue.js組件使用props傳遞數(shù)據(jù)的方法
相關(guān)文章
vue3 el-pagination 將組件中英文‘goto’ 修改 為&nbs
這篇文章主要介紹了vue3 el-pagination 將組件中英文‘goto’ 修改 為 中文到‘第幾’,通過實(shí)例代碼介紹了vue3項(xiàng)目之Pagination 組件,感興趣的朋友跟隨小編一起看看吧2024-02-02vue中PC端使用高德地圖實(shí)現(xiàn)搜索定位、地址標(biāo)記、彈窗顯示定位詳情(完整實(shí)例)
這篇文章主要介紹了vue中PC端使用高德地圖實(shí)現(xiàn)搜索定位、地址標(biāo)記、彈窗顯示定位詳情,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07vue中報(bào)錯(cuò)“error‘xxx‘?is?defined?but?never?used”問題及解決
介紹了兩種解決代碼導(dǎo)入問題的方法:單一代碼解決和全局解決,第一種方法是在代碼前面添加特定代碼并保存;第二種方法是在package.json中添加代碼后重啟項(xiàng)目,這些方法可以有效解決導(dǎo)包錯(cuò)誤提示,希望對(duì)大家有幫助2024-10-10vue移動(dòng)端使用canvas簽名的實(shí)現(xiàn)
這篇文章主要介紹了vue移動(dòng)端使用canvas簽名的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01