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

Vue中computed計算屬性和data數(shù)據(jù)獲取方式

 更新時間:2022年03月03日 16:39:23   作者:米柆  
這篇文章主要介紹了Vue中computed計算屬性和data數(shù)據(jù)獲取方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

computed計算屬性和data數(shù)據(jù)獲取

獲取到數(shù)據(jù)(對象、數(shù)組),截取一部分顯示到頁面中,用computed計算屬性來實現(xiàn)截取數(shù)據(jù)然后直接輸出到頁面。

<div class="detailBox">
  <h1 class="detailHead">{{ActiveData.title}}</h1>
  <div class="detailCon">
    <p><b>活動時間:</b>{{ActStart}} 至 {{ActEnd}}</p>
    <p><b>報名時間:</b>{{SigStart}} 至 {{SigEnd}}</p>
  </div>
</div>
data() {
   return {
     ActiveData:"",//活動詳情所有數(shù)據(jù)
  }
},
methods:{
//獲取對應的數(shù)據(jù)
    this.ActiveData = response.data.data;
}
computed:{
    ActStart(){
      console.log(this.ActiveData.activity_starttime);
      return this.ActiveData.activity_starttime.substring(5,11);
    },
    ActEnd(){
      return this.ActiveData.activity_endtime.substring(5,11);
    },
    SigStart(){
     return this.ActiveData.signup_starttime.substring(5,11);
    },
    SigEnd(){
      return this.ActiveData.signup_endtime.substring(5,11);
    },
  }

頁面如愿顯示出想要的效果了,但是也報錯了!那是因為data里的數(shù)據(jù)是在mouted中執(zhí)行函數(shù)才獲取到數(shù)據(jù),是在computed之后,所以在第一次computed計算時,data中的ActiveData數(shù)據(jù)還是空的,所以computed找不到ActiveData里的數(shù)據(jù)。

就會報undefinded接著是Error in render: "TypeError:……"獲取到值后重新計算又出現(xiàn)了獲取到的值。

解決方法一

給要截取的數(shù)據(jù)賦一個默認值,computed計算屬性會先去計算默認值,在獲取到新值后重新計算,就不會報undefinded的錯誤了。

data() {
    return {
      ActiveData:"",//活動詳情所有數(shù)據(jù)
      ActStarts:"",//活動開始時間
      ActEnds:"",//活動結(jié)束時間
      SigStarts:"",//報名開始時間
      SigEnds:"",//報名結(jié)束時間
    }
  }, 
 
methods:{
//獲取對應的數(shù)據(jù)
    this.ActiveData = response.data.data;
    this.ActStarts = response.data.data.activity_starttime;
    this.ActEnds = response.data.data.activity_endtime;
    this.SigStarts = response.data.data.signup_starttime
    this.SigEnds = response.data.data.signup_endtime
}
computed:{
    ActStart(){
      console.log(this.ActStarts);
      return this.ActStarts.substring(5,11);
    },
    ActEnd(){
      return this.ActEnds.substring(5,11);     
    },
    SigStart(){
      return this.SigStarts.substring(5,11);
    },
    SigEnd(){
      return this.SigEnds.substring(5,11);
    },
  }

解決方法二

在computed中增加if判斷,在data中的ActiveData里有數(shù)據(jù)時才在computed中返回對應的數(shù)據(jù)

data() {
   return {
     ActiveData:"",//活動詳情所有數(shù)據(jù)
  }
},
methods:{
//獲取對應的數(shù)據(jù)
    this.ActiveData = response.data.data;
}
computed:{
    ActStart(){
      console.log(this.ActiveData.activity_starttime);
      if(this.ActiveData.activity_starttime !== undefined){
        return this.ActiveData.activity_starttime.substring(5,11);
      }
   },
   ActEnd(){
      if(this.ActiveData.activity_endtime !== undefined){
        return this.ActiveData.activity_endtime.substring(5,11);
      }
   },
   SigStart(){
      if(this.ActiveData.signup_starttime !== undefined){
        return this.ActiveData.signup_starttime.substring(5,11);
      }
   },
   SigEnd(){
       if(this.ActiveData.signup_endtime !== undefined){
        return this.ActiveData.signup_endtime.substring(5,11);
      }
   },
}

computed計算屬性取對象的值,第一次報錯undefined

代碼如下:

  data() {
    return {
      limit:3,
      checkedIndex:0,
      addressList:[],
      isMdShow:false,
      addressId:""
    };
  },
  components: {
    NavHeader,
    NavFooter,
    NavBread,
    Modal
  },
  mounted(){
    this.init()
  },
    computed:{
    addressListFilter(){
      return this.addressList.slice(0,this.limit)
    },
    selectedAddressId(){
      
      // if(this.addressList.length>0){
       let data = this.addressList[this.checkedIndex].addressId
       console.log(this.addressList,"====")
       return data
      //  }
    }
  },

圖片:

注意,初始化的時候,addressList還是一個空數(shù)組,那addressList[index]對象就不存在,肯定就沒有addressId這個屬性,所以會報undefined

報錯和打印值

解決方案

那我們現(xiàn)在可以知道,報錯的原因是第一次計算的時候,addressList還沒有賦值,所以,我們可以進行一個判斷,當addressList有值了我們在進行計算

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

相關文章

  • BeanUtils.copyProperties復制屬性失敗的原因及解決方案

    BeanUtils.copyProperties復制屬性失敗的原因及解決方案

    這篇文章主要介紹了BeanUtils.copyProperties復制屬性失敗的原因及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Spring Data Envers支持有條件變動紀錄的保存和查詢的方法

    Spring Data Envers支持有條件變動紀錄的保存和查詢的方法

    通過spring-data-envers可以很容易的實現(xiàn)數(shù)據(jù)變動紀錄的保存和查詢,本文介紹支持有條件變動紀錄的保存和查詢的方法,通過spring-data-envers很容易的實現(xiàn)變動紀錄的保存和查詢,只需要增加幾個注解就可以,感興趣的朋友跟隨小編一起看看吧
    2023-10-10
  • Linux centos7環(huán)境下jdk安裝教程

    Linux centos7環(huán)境下jdk安裝教程

    這篇文章主要為大家詳細介紹了Linux centos7環(huán)境下jdk的安裝教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Java concurrency線程池之Callable和Future_動力節(jié)點Java學院整理

    Java concurrency線程池之Callable和Future_動力節(jié)點Java學院整理

    這篇文章主要為大家詳細介紹了Java concurrency線程池之Callable和Future,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Java集合之LinkedHashSet集合詳解

    Java集合之LinkedHashSet集合詳解

    這篇文章主要介紹了Java集合之LinkedHashSet集合詳解,具有可預知迭代順序的Set接口的哈希表和鏈表列表實現(xiàn),此實現(xiàn)與HashSet不同的是,后者維護著一個運行于所有條目的雙重鏈表列表,此鏈表定義了迭代順序,需要的朋友可以參考下
    2023-09-09
  • Effective Java 在工作中的應用總結(jié)

    Effective Java 在工作中的應用總結(jié)

    《Effective Java》是一本經(jīng)典的 Java 學習寶典,值得每位 Java 開發(fā)者閱讀。下面文章即是將書中和平日工作較密切的知識點做了部分總結(jié),需要的朋友可以參考下
    2021-09-09
  • java編寫簡易貪吃蛇游戲

    java編寫簡易貪吃蛇游戲

    這篇文章主要為大家詳細介紹了java編寫簡易貪吃蛇游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • 解決Eclipse Tomcat OutOfMemoryError:PermGen space的問題

    解決Eclipse Tomcat OutOfMemoryError:PermGen space的問題

    今天小編就為大家分享一篇關于解決Eclipse Tomcat OutOfMemoryError:PermGen space的問題,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • springboot Mongodb的集成與使用實例詳解

    springboot Mongodb的集成與使用實例詳解

    這篇文章主要介紹了springboot Mongodb的集成與使用實例詳解,需要的朋友可以參考下
    2018-04-04
  • Mybatis?plus?where添加括號方式

    Mybatis?plus?where添加括號方式

    這篇文章主要介紹了Mybatis?plus?where添加括號方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01

最新評論

舒城县| 定州市| 阿克陶县| 建阳市| 承德市| 瑞丽市| 日喀则市| 弥勒县| 水城县| 乌恰县| 乌鲁木齐县| 山东省| 十堰市| 利津县| 陕西省| 瓦房店市| 吉隆县| 芜湖县| 平谷区| 平舆县| 四子王旗| 桂平市| 三明市| 中西区| 融水| 云梦县| 东兰县| 微博| 闵行区| 海兴县| 阿巴嘎旗| 柳林县| 秦皇岛市| 东丽区| 循化| 云安县| 隆子县| 惠州市| 杨浦区| 泾阳县| 仙游县|