vue中mounted內如何調完異步方法再渲染


問題描述:我們在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()”是調用的后台的一個方法。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM