Vue js 在 ajax 调用后设置数据
Vue js Set data after ajax call
我有一个显示用户信息的地方,当页面刷新时,我从 ajax 调用中读取用户信息并设置用户信息 data.but 因为 axios 调用的耗时过程数据设置是在 DOM 渲染之后执行。
HTML :
<div> {{user.name}} </div>
vue.js :
data: function(){
return {
user: {},
}
},
methods: {
getUserInfo() {
axios.post(laroute.route('link'), data).then((response) => {
this.$set(this, 'user', response.data.user);
},
},
mounted() {
this.getUserInfo();
}
刷新页面后出现此错误:
"TypeError: Cannot read property 'name' of undefined"
我无法添加评论。您可以尝试更改:
为创建安装
和
this.$set(this, 'user', response.data.user);对于用户 = response.data.user
总之,你能console.log调用结果吗?
我有一个显示用户信息的地方,当页面刷新时,我从 ajax 调用中读取用户信息并设置用户信息 data.but 因为 axios 调用的耗时过程数据设置是在 DOM 渲染之后执行。
HTML :
<div> {{user.name}} </div>
vue.js :
data: function(){
return {
user: {},
}
},
methods: {
getUserInfo() {
axios.post(laroute.route('link'), data).then((response) => {
this.$set(this, 'user', response.data.user);
},
},
mounted() {
this.getUserInfo();
}
刷新页面后出现此错误:
"TypeError: Cannot read property 'name' of undefined"
我无法添加评论。您可以尝试更改:
为创建安装 和 this.$set(this, 'user', response.data.user);对于用户 = response.data.user
总之,你能console.log调用结果吗?