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è)務需求確實需要修改,那么請賦值props的內(nèi)容到data中一份,然后去修改data中的數(shù)據(jù)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue中使用vue-count-to(數(shù)字滾動插件)詳細教程
這篇文章主要給大家介紹了關(guān)于Vue中使用vue-count-to(數(shù)字滾動插件)的相關(guān)資料,最近需要開發(fā)一個數(shù)字滾動效果,在網(wǎng)上找到一個關(guān)于vue-countTo的插件,覺得這個插件還不錯,需要的朋友可以參考下2023-09-09
Vue3 核心特性Suspense 與 Teleport 原理解析
本文詳細解析了Vue3的核心特性Suspense和Teleport,包括它們的實現(xiàn)原理、核心源碼、生命周期流程、依賴追蹤機制等,通過深入理解這兩個API的設(shè)計哲學,開發(fā)者可以提升代碼組織能力、優(yōu)化應用性能和實現(xiàn)更優(yōu)雅的架構(gòu)設(shè)計,感興趣的朋友一起看看吧2025-03-03

