我想做这样的事情mounted() {}:
await fetchData1();
await fetchData2UsingData1();
doSomethingUsingData1And2();
Run Code Online (Sandbox Code Playgroud)
所以我想知道这是否有效:
async mounted() {
await fetchData1();
await fetchData2UsingData1();
doSomethingUsingData1And2();
},
Run Code Online (Sandbox Code Playgroud)
在我的环境中它没有引起任何错误,并且似乎运行良好.但是在这个问题中,async/await生命周期钩子没有实现.
https://github.com/vuejs/vue/issues/7209
我找不到更多信息,但实际上是否可用?
阅读过Alligator.io关于Vue的一篇文章后,他说安装的生命周期是使用http get的不好的地方。我想知道是否有关于如何正确地从Vue.js中的API获取数据的准则?
我已经初始化了数据。
data () {
return {
current_product: {},
current_ID: '',
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我从生命周期创建挂钩上的 REST API 获取数据。
created () {
var skuID = this.$store.state.selected_productSKU.productSKU_ID
axios.get(`http://localhost:8081/api/products/${skuID}`)
.then(response => {
this.current_ID = response.data.product_ID
this.current_product = response.data
})
.catch(e => {
alert(e)
})
}
Run Code Online (Sandbox Code Playgroud)
最后,我使用计算属性来获取一些值
// THIS JUST RETURN ['XL', 'M']
focusedProduct_SKUS_NoDupSizes () {
var newArr = this.current_product.product_SKU.filter((sku, index, self) =>
index === self.findIndex(t => (
t.productSKU_size === sku.productSKU_size
))
)
var x = newArr.map(a => a.productSKU_size)
return x
}
Run Code Online (Sandbox Code Playgroud)
vue 实例显示预期结果 …