【发布时间】:2021-07-21 16:10:09
【问题描述】:
我正在尝试根据另一个名为 Product 的反应变量中的数据设置 Form 的值,但它似乎不起作用。 Form 应将其值设置为 Product 数据(如果可用),如果不可用,则使用 null。
这是一段vue组件代码:
props: {
ProductID: {
type: Number,
default: null
}
},
setup (props) {
const Product = ref();
async function loadProduct(ProductID) { // Runs when `props.ProductID` changes using a watcher
try {
const Response = await $axios.get(`/api/product/${ProductID}`);
Product.value = Response.data; // This is an array of data
} catch (error) {
console.log(error);
}
}
}
const Form = ref({
ProductTitle: Array.isArray(Product.value) && Product.value[0].producttitle ? Product.value[0].producttitle : null,
ProductText: Array.isArray(Product.value) && Product.value[0].producttext ? Product.value[0].producttext : null,
)}
}
在 vue devtools 中,我可以看到 Product 填充了数据,但这些值从未分配给 Form 键 - 它们只是空值。如果Product 是响应式的,Form 也是响应式的,为什么这不起作用?
【问题讨论】:
标签: vue.js vuejs3 vue-composition-api