2 回答

TA貢獻1866條經驗 獲得超5個贊
Props 驗證是在組件初始化之前完成的,因此this您在擴展Vue.prototype.
形成他們的文件:
請注意,在創建組件實例之前驗證 props,因此實例屬性(例如數據、計算等)在默認或驗證器函數中將不可用。
一般來說,如果$checkProps只用于檢查這些 props 的值,我只會使用一個輔助函數。
// array.helpers.js
export function containsValue(arr, val) {
return arr.indexOf(value) !== -1
}
// component
import { containsValue } from 'path/to/helpers/array.helpers';
props: {
foo: {
//
validator(value) {
return containsValue(['foo', 'bar'], value);
}
}
}
更新
根據您的評論,如果您不想一遍又一遍地導入此特定功能,則可以Array.prototype.includes 查看文檔
// component
props: {
color: {
//
validator(value) {
return ['success', 'danger'].includes(value);
}
}
}
添加回答
舉報