我的 Vue 應用程式中有這段代碼:我想做的是根據道具更改顏色屬性。我也使用計算,因為顏色屬性可能會在組件之外更改。
export default defineComponent({
name: 'mail-dropdown',
props: {
title: {
type: String,
default: '',
// required: true
},
id: {
type: String,
},
shade: {
type: String,
default: 'white',
},
grey: {
type: Boolean,
default: false,
// required: true
},
items: { type: Object as PropType<ActionItem[]> },
},
setup(props, context) {
const store = useCounterStore();
let color = computed(() => props.shade);
if (props.grey == true) {
color = 'grey-5';
};
return { store, color };
},
});
我在這里遇到一個錯誤:
if (props.grey == true) {
color = 'grey-5';
};
錯誤如下:
> let color: ComputedRef<string>
Type 'string' is not assignable to type 'ComputedRef<string>'.
改變的解決方案是color什么?
uj5u.com熱心網友回復:
您錯過了添加.value:
if (props.grey == true) {
color.value = 'grey-5';
};
但計算屬性應該是可寫的:
let color = computed({
get(){
return props.shade
},
set(val){
//emit the new val or mutate the store state
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/521540.html
