问题描述:我们在mounted内执行异步方法,会先执行后边的语句,这就导致取不到想要的值。

方法:我们在方法中加入回调函数,将mounted的同步语句放在回调函数内,渲染语句套上$nextTick。

示例

     data() {
            return {
                carouselArr:[],
            }
        },
        methods: {
           getList(callback) {
            fun().then(res => {
              this.carouselArr = res.data;
         console.log(this.carouselArr);
              // 回调
              callback(this.carouselArr);
            })
          }
        },
        mounted() {
          var _this = this;
          this.getList(function (arr) {
              _this.$nextTick(() => {
                    console.log(arr);
              });
          });
        }                   

 

示例中出现的“fun()”是调用的后台的一个方法。

 

相关文章:

  • 2022-12-23
  • 2021-12-09
  • 2023-03-24
  • 2021-04-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-29
  • 2022-01-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案