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

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

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

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

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

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

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

TypeError: Cannot set property ‘tableData‘ of undefined

問(wèn)題背景

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

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

分析

代碼看起來(lái),好像并沒(méi)有什么毛病。。。

export default {
  data() {
    return {
      total: 0, //默認(rèn)數(shù)據(jù)總數(shù)
      searchParam:{
        limit: 10, //每頁(yè)的數(shù)據(jù)條數(shù)
        page: 1, //默認(rèn)開始頁(yè)面
        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);
      });
    }
};

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

解決方案

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

onSearch: function(){
      const that=this; //用that解決函數(shù)內(nèi)部this指向問(wèn)題 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);
      });
    }

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

相關(guān)文章

最新評(píng)論

开原市| 凤城市| 中山市| 凤冈县| 海口市| 舒城县| 陵水| 夏河县| 夹江县| 宁津县| 出国| 广东省| 松原市| 余姚市| 会泽县| 海原县| 应用必备| 广南县| 文化| 山西省| 乐陵市| 博白县| 武穴市| 新安县| 盐城市| 安达市| 永宁县| 雷山县| 岳池县| 郑州市| 滨州市| 民勤县| 泉州市| 伊金霍洛旗| 广河县| 菏泽市| 鄯善县| 田林县| 嘉义市| 浦东新区| 宁陕县|