javascript - Vue js ajax调用后设置数据
问题描述
我有一个显示用户信息的地方,当页面刷新时,我从 ajax 调用中读取用户信息并设置用户信息数据。但是由于 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"
解决方案
我无法添加评论。您可以尝试更改:
安装为 created 和 this.$set(this, 'user', response.data.user); 对于用户 = response.data.user
无论如何,您可以 console.log 调用结果吗?
推荐阅读
- javascript - 使用和不使用构造函数将函数传递给状态的差异
- c - ((size_t *)(vec))[-1] 是否违反严格混叠?
- sql-server - 无法从运行时连接管理器获取托管连接
- oracle - Yii2:跨两个数据库查询
- javascript - 如何在fabricjs画布对象上触发mousedown事件?
- java - 为什么 Java Spec 建议将 int... 用于形式参数,将 int[] 用于功能接口?
- mysql - 将 MS SQL Server 性能查询转换为 MySQL
- node.js - React.js create-react-app Npm 运行构建 - 失败
- php - 如何调整多个图像的大小php
- java - java和Scala中非Asci字符的子字符串