Vue的子組件props如何設(shè)置多個校驗類型
vue子組件props設(shè)置多個校驗值
1. type使用 | 進(jìn)行隔開
props: {
? ? iconClass: {
? ? ? type: String | null,
? ? ? required: true,
? ? ? default: ""
? ? }
},2. 使用數(shù)組
props: {
? iconClass: [String , null]
},3. 使用validator校驗函數(shù)
props: {
?? ?iconClass: {
?? ? ? ?validator: (value)=> {
?? ? ? ? ?const getResult = Object.prototype.toString.call(value)
?? ? ? ? ?if(getResult === "[object Null]" || getResult === "[object String]") return true;
?? ? ? ?},
?? ? ? ?required: true,
?? ? ? ?default: ""
? },
}vue組件參數(shù)校驗
在vue中,當(dāng)父組件向子組件傳遞值時.子組件可以對傳遞過來的值進(jìn)行參數(shù)校驗.
首先我們定義一個子組件child,接受父組件傳遞過來的值content.
<child :content="1"></child>
Vue.component('child',{
? ? ? ? ? ? ? props:['content'],
? ? ? ? ? ? ? template: "<div>{{content}}</div>",
? ? ? ? ? })注意但我們在content前面加上:,它會認(rèn)為這是js表達(dá)式,所以認(rèn)為"1"是Number類型而不是String類型.
參數(shù)校驗一
限定參數(shù)的類型
<child :content="1"></child>
Vue.component('child',{
? ? ? ? ? ? ? props:{
? ? ? ? ? ? ? ?content: [String,Number], ? //這樣就限制了參數(shù)的類型為String或者Number.
? ? ? ? ? ? ?},
? ? ? ? ? ? ? template: "<div>{{content}}</div>",
? ? ? ? ? })如果不滿足則會報[Vue warn]: Invalid prop: type check failed for prop “content”. Expected String, got Number.
參數(shù)校驗二
限定參數(shù)的類型,是否必須,默認(rèn)值
?Vue.component('child',{
? ? ? ? ? ? ? props:{
? ? ? ? ? ? ? ? ?content:{
? ? ? ? ? ? ? ? ? ? ?type:Number, ? //限制參數(shù)的類型為Number
? ? ? ? ? ? ? ? ? ? ?default:100, ? //設(shè)置參數(shù)的默認(rèn)值為100
? ? ? ? ? ? ? ? ? ? ?required:false, ?//是否必須
? ? ? ? ? ? ? ? ?}?
? ? ? ? ? ? ? },
? ? ? ? ? ? ? template: "<div>{{content}}</div>",
? ? ? ? ? })參數(shù)校驗三
自定義校驗規(guī)則
Vue.component('child',{
? ? ? ? ? ? ? props:{
? ? ? ? ? ? ? ? ?content:{
? ? ? ? ? ? ? ? ? ? ?type:Number,
? ? ? ? ? ? ? ? ? ? ?default:100,
? ? ? ? ? ? ? ? ? ? ?required:false,
? ? ? ? ? ? ? ? ? ? ?validator:function(value){ ? //自定義校驗的規(guī)則
? ? ? ? ? ? ? ? ? ? ? ? ?return value>5;
? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? },
? ? ? ? ? ? ? template: "<div>{{content}}</div>",
? ? ? ? ? })總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3 el-pagination 將組件中英文‘goto’ 修改 為&nbs
這篇文章主要介紹了vue3 el-pagination 將組件中英文‘goto’ 修改 為 中文到‘第幾’,通過實例代碼介紹了vue3項目之Pagination 組件,感興趣的朋友跟隨小編一起看看吧2024-02-02
vue網(wǎng)站優(yōu)化實戰(zhàn)之秒開網(wǎng)頁
最近在搭建自己的博客,前端采用Vue技術(shù),發(fā)現(xiàn)首頁加載速度非常之慢,常常達(dá)到10S左右,遂開始優(yōu)化之旅,這篇文章主要給大家介紹了關(guān)于vue網(wǎng)站優(yōu)化實戰(zhàn)之秒開網(wǎng)頁的相關(guān)資料,需要的朋友可以參考下2022-08-08
關(guān)于vuepress部署出現(xiàn)樣式的問題及解決
這篇文章主要介紹了關(guān)于vuepress部署出現(xiàn)樣式的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
element table列表根據(jù)數(shù)據(jù)設(shè)置背景色
在使用elementui中的el-table時,需要將表的背景色和字體顏色設(shè)置為新顏色,本文就來介紹一下element table列表根據(jù)數(shù)據(jù)設(shè)置背景色,感興趣的可以了解一下2023-08-08
使用vue.js實現(xiàn)checkbox的全選和多個的刪除功能
這篇文章主要介紹了使用vue.js實現(xiàn)checkbox的全選和多個的刪除功能,需要的朋友可以參考下2017-02-02
Vue電商網(wǎng)站首頁內(nèi)容吸頂功能實現(xiàn)過程
電商網(wǎng)站的首頁內(nèi)容會比較多,頁面比較長,為了能讓用戶在滾動瀏覽內(nèi)容的過程中都能夠快速的切換到其它分類。需要分類導(dǎo)航一直可見,所以需要一個吸頂導(dǎo)航的效果。目標(biāo):完成頭部組件吸頂效果的實現(xiàn)2023-04-04

