我试图实现与 Vue 中的 ...mapState()
函数相同的功能。
我有一个 axios 请求,它返回一个具有不同属性的对象,如下所示:
axios.get(
'/'
).then(
res => {
console.log(res) // {name: 'foo', age: 22, height: '1.72m', job: 'developer'}
}
)
现在,以与 ...mapState()
相同的方式,我可以提取 Prop ,以便我可以在我的模板中使用它们,例如:
<template>
<div>
Hello My name is {{name}} and I am {{age}} years old
</div>
</template>
我正在考虑将响应对象分配给 vuejs 数据,但我还有其他变量不想被重写
请您参考如下方法:
你可以试试这个:
axios.get(
'/'
).then(
res => {
console.log(res) // {name: 'foo', age: 22, height: '1.72m', job: 'developer'}
return { index: res}
}
)
然后您就可以访问数据
<template>
<div>
Hello My name is {{index.name}} and I am {{index.age}} years old
</div>
</template>