vue props數(shù)據(jù)傳遞類型限制方式
vue props數(shù)據(jù)傳遞類型限制
1.第一種:不限制任何數(shù)據(jù)
props: ['test', 'test1'],?
2.第二種:只限制數(shù)據(jù)類型
props: {
? ? test: String,
? ? test1: Number
? },?3.第三種:限制數(shù)據(jù)類型 必要參 默認值
? props: {
? ? test: {
? ? ? type: String, // 字符類型
? ? ? required: true // 必要條件
? ? },
? ? test1: {
? ? ? type: Number, // 字符類型
? ? ? default: 99 // 默認值
? ? },
? },Vue中的配置項props
功能:讓組件接收外部傳入的數(shù)據(jù) 向子組件傳遞數(shù)據(jù)
(1).傳遞數(shù)據(jù)(父組件傳遞數(shù)據(jù))
< Student name="李四" :age="18" sex="女"/>
(2).接收數(shù)據(jù)(子組件接收數(shù)據(jù))
第一種方式(只接收)
簡單的聲明接收
props:['name','sex','age']?
第二種方式(限制類型)
接收的同時對數(shù)據(jù)進行類型限制
?props:{
? ? ? ? ? ?name:String,
? ? ? ? ? ?age:Number,
? ? ? ? ? ?sex:String
? ? ? ? ?}?第三種方式(限制類型、限制必要性、指定默認值)
接收數(shù)據(jù)的同時:進行類型數(shù)據(jù)限制+默認值+必要性的限制
?? ?props:{
? ? ? ? ? ? ? ? name:{
? ? ? ? ? ? ? ? ? ? type:String, //name的類型是字符串
? ? ? ? ? ? ? ? ? ? required:true //name是必要的
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? age:{
? ? ? ? ? ? ? ? ? ? type:Number,
? ? ? ? ? ? ? ? ? ? default:99 //默認值
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? sex:{
? ? ? ? ? ? ? ? ? ? type:String,
? ? ? ? ? ? ? ? ? ? required:true
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }備注:props是只讀的,Vue底層會檢測你對props的修改,如果進行了修改。就會發(fā)出警告,
若業(yè)務(wù)需求確實需要修改,那么請賦值props的內(nèi)容到data中一份,然后去修改data中的數(shù)據(jù)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue?url跳轉(zhuǎn)解析和參數(shù)編碼介紹
這篇文章主要介紹了vue?url跳轉(zhuǎn)解析和參數(shù)編碼,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
Vue數(shù)據(jù)驅(qū)動模擬實現(xiàn)3
這篇文章主要為大家詳細介紹了Vue數(shù)據(jù)驅(qū)動模擬實現(xiàn),教大家如何在某個對象中,新增某個屬性,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
Vue.js結(jié)合Ueditor富文本編輯器的實例代碼
本篇文章主要介紹了Vue.js結(jié)合Ueditor的項目實例代碼,這里整理了詳細的代碼,具有一定的參考價值,有興趣的可以了解一下2017-07-07
vue中提示$index is not defined錯誤的解決方式
這篇文章主要介紹了vue中提示$index is not defined錯誤的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
jenkins自動構(gòu)建發(fā)布vue項目的方法步驟
這篇文章主要介紹了jenkins自動構(gòu)建發(fā)布vue項目的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01

