javascript - 网页在 Axios GET 响应之前加载
问题描述
我对 Vue.js 很陌生。
可能有人可以帮助我解决下一个问题:
我的网页元素的值取自我应该在页面加载之前发送的 Axios GET 响应。但是我的页面在我得到服务器的响应之前就已经加载了。我收到一个错误,即值未定义。
我的 main.vue 文件
beforeMount()
{
this.firstTimeRun();
},
我的 mixin.js 文件
methods: {
async firstTimeRun() {
await this.getConfig();
await this.getAlarmStatus();
},
async getConfig() {
const configResponce = await axios.get('http://10.10.10.10/multi_get.json?c0=config')
.then((res => { this.config = res; }))
.catch(err => console.log(err));
this.$store.commit("setConfigData", this.config);
},
async getAlarmStatus() {
const alarmResponce = await axios.get('http://10.10.10.10/multi_get.json?c0=alarm_status')
.then((res => {
this.alarm_status = res;
}))
.catch(err => console.log(err));
this.$store.commit("setAlarmData", this.alarm_status);
},
// In this method I fill the elements and I suppose to use data from getConfig() and
getAlarmStatus() methods.
fillElements(){
// Do something...
}
}
有人对此有答案吗?谢谢。
解决方案
当您调用 get 方法时,添加 e.preventDefault 方法。单击提交按钮时,它将停止页面加载。
async getConfig(e) {
e.preventDefault();
const configResponce = await axios.get('http://10.10.10.10/multi_get.json?c0=config')
.then((res => { this.config = res; }))
.catch(err => console.log(err));
this.$store.commit("setConfigData", this.config);
},
推荐阅读
- r - 在 MATRIC 的多列上使用 FOR LOOP 并在 RStudio 中保持 FIRST 列不变
- reactjs - React 和 Redux 模式
- reactjs - react-three-fiber中如何使用instancedBufferAttribute来绘制粒子?
- java - 结合两个单声道和返回值
- spring-boot - 带有@QuerydslPredicate 的逗号分隔的长查询参数未正确映射
- wpf - 绑定itemsource后如何清除WPF ItemList?
- java - 无法连接到 AWS RDS 数据库 - Java MySQL 连接错误
- python - 无法用 Pandas 填充 Dataframe 的特定单元格
- python - 在灰度低分辨率图像上书写文本
- python - OSError: [Errno 22] 无效参数 tryign 克隆到 github 页面