Vue Composition API - 使用 computed 在 setup() 中转换数据?
Vue Composition API - Using computed which transforms data in setup()?
我们目前正在从样板 VUE 2 转换为组合 API,我正在努力了解如何重写我们当前的 computed 以支持组合 API:
setup() {
const store = useStore<StoreState>();
// Question : how do i implement the infoFields into the setup ?
// const contactSingle = computed(() => store.state.contacts.contactSingle);
return { contactSingle };
},
computed: {
...mapGetters("contacts", ["getContact"]),
infoFields(): any {
return [
{
value: (this as any).getContact.customer.firstName,
label: "Fornavn",
},
{
value: (this as any).getContact.customer.lastName,
label: "Etternavn",
},
...
...
];
},
<v-row>
<v-col class="pt-0" v-for="(item, i) in infoFields" :key="i + '-field'" cols="12" xs="12" sm="6" md="6" lg="4">
<BaseSheetField :value="item.value" :label="item.label" />
</v-col>
</v-row>
不确定到底是什么问题,但我认为在计算中使用 store.getters
应该可行:
const infoFields = computed(() => {
return [
{
value: store.getters["contacts/getContact"].customer.firstName,
label: "Fornavn",
},
{
value: store.getters["contacts/getContact"].customer.lastName,
label: "Etternavn",
}
]
})
我们目前正在从样板 VUE 2 转换为组合 API,我正在努力了解如何重写我们当前的 computed 以支持组合 API:
setup() {
const store = useStore<StoreState>();
// Question : how do i implement the infoFields into the setup ?
// const contactSingle = computed(() => store.state.contacts.contactSingle);
return { contactSingle };
},
computed: {
...mapGetters("contacts", ["getContact"]),
infoFields(): any {
return [
{
value: (this as any).getContact.customer.firstName,
label: "Fornavn",
},
{
value: (this as any).getContact.customer.lastName,
label: "Etternavn",
},
...
...
];
},
<v-row>
<v-col class="pt-0" v-for="(item, i) in infoFields" :key="i + '-field'" cols="12" xs="12" sm="6" md="6" lg="4">
<BaseSheetField :value="item.value" :label="item.label" />
</v-col>
</v-row>
不确定到底是什么问题,但我认为在计算中使用 store.getters
应该可行:
const infoFields = computed(() => {
return [
{
value: store.getters["contacts/getContact"].customer.firstName,
label: "Fornavn",
},
{
value: store.getters["contacts/getContact"].customer.lastName,
label: "Etternavn",
}
]
})