最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue實(shí)現(xiàn)動態(tài)給data函數(shù)中的屬性賦值

 更新時間:2022年09月07日 12:00:13   作者:夜觀山海  
這篇文章主要介紹了vue實(shí)現(xiàn)動態(tài)給data函數(shù)中的屬性賦值,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

vue動態(tài)給data函數(shù)中的屬性賦值

1.首先創(chuàng)建一個監(jiān)視器,用來監(jiān)視相關(guān)的屬性

2.當(dāng)這個相關(guān)的屬性被修改的時候,在監(jiān)視器中的handler函數(shù)中寫處理邏輯即可

vue給data中的數(shù)據(jù)賦值報(bào)錯

TypeError: Cannot set property ‘tableData‘ of undefined

問題背景

最近剛?cè)腴Tvue,有個典型的場景,也是簡單的坑。就是需要通過axios請求數(shù)據(jù)之后,將數(shù)據(jù)賦予到data中的tableData,而table組件會根據(jù)tableData雙向綁定自動渲染。

但是當(dāng)我賦值的時候TypeError: Cannot set property 'tableData' of undefined。究竟怎么回事呢。

分析

代碼看起來,好像并沒有什么毛病。。。

export default {
  data() {
    return {
      total: 0, //默認(rèn)數(shù)據(jù)總數(shù)
      searchParam:{
        limit: 10, //每頁的數(shù)據(jù)條數(shù)
        page: 1, //默認(rèn)開始頁面
        certNumber: "",
        certLevel: "",
        certCompanyName: "",
      },
      tableData: []
    };
  },
onSearch: function(){
      axios.post('/api/certCompany/list2',JSON.stringify(this.searchParam))
      .then(function (response) {
        console.log(response);
        this.tableData=response.data.data;
        this.total=response.data.count;
      })
      .catch(function (error) {
        console.log(error);
      });
    }
};

然而問題是出來了this.tableData因?yàn)橛昧?function 函數(shù) 的原因,在函數(shù)里面,this是指向函數(shù)本身,已經(jīng)不是外部的默認(rèn)this了

解決方案

使用一個 that 指向 外部的this ,然后調(diào)用 that.tableData ,輕松搞定。

onSearch: function(){
      const that=this; //用that解決函數(shù)內(nèi)部this指向問題 zhengkai.blog.csdn.net
      axios.post('/api/certCompany/list2',JSON.stringify(this.searchParam))
      .then(function (response) {
        console.log(response);
        that.tableData=response.data.data;
        that.total=response.data.count;
      })
      .catch(function (error) {
        console.log(error);
      });
    }

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

西丰县| 清镇市| 河北区| 慈溪市| 洪湖市| 南丹县| 澜沧| 清新县| 花莲市| 阿城市| 宿州市| 桐柏县| 洪江市| 依兰县| 广南县| 长宁县| 宝清县| 敖汉旗| 读书| 东兰县| 缙云县| 锡林浩特市| 吴江市| 平凉市| 合川市| 突泉县| 安康市| 四会市| 吉安县| 资中县| 陆川县| 呈贡县| 晋中市| 鄂托克前旗| 漾濞| 盘锦市| 当雄县| 嘉峪关市| 平塘县| 包头市| 鄂托克前旗|