【问题标题】:mounted function calling multiple times vue.js挂载函数多次调用vue.js
【发布时间】:2021-08-17 13:22:34
【问题描述】:

当我的页面第一次加载时,我的 vue.js 页面中有一个挂载函数 this.editId 变量包含值,但第二次和第三次也自动调用挂载函数,这表明

app.js:1088 GET http://localhost:8000/api/backend/bill/undefined 404 (Not Found)

但我第一次在控制台中获取 editId 值。

  export default {
      components: {
            Datepicker,datetime
        },
      async mounted(){
           const header = {
                      'Authorization': 'Bearer ' + this.LoginUserData,
                };
            this.editId = this.$route.params.id;
             try{   
             console.log(this.editId);
             let response = await axios.get(`/api/backend/bill/${this.editId}` ,{headers: header});
             this.form = response.data;
             }catch(err){
              
             }
        },

任何解决方案,谢谢

【问题讨论】:

  • 抱歉,我想确认一下。您的问题是该挂载多次调用吗?还是多次刷新页面时this.editId的值有问题?

标签: vue.js vuejs2 vue-router


【解决方案1】:

尝试检查

if (this.$route.params.id)
  const header = {
    'Authorization': 'Bearer ' + this.LoginUserData,
  };
  this.editId = this.$route.params.id;
   try {   
     let response = await axios.get(`/api/backend/bill/${this.editId}` ,{headers: header});
     this.form = response.data;
   } catch(err) {
     console.log(err)
   }
}

然后进行 api 调用

【讨论】:

  • 谢谢我已经尝试过这个用于尝试块的解决方案,但仍然mounted被多次调用
  • 如果变量未定义,它仍在移动到 else 部分
猜你喜欢
  • 2020-03-23
  • 1970-01-01
  • 2018-06-14
  • 2021-01-09
  • 1970-01-01
  • 2016-09-03
  • 2021-08-02
  • 2020-11-27
相关资源
最近更新 更多