鍍金池/ 問答/HTML/ vue 組件初始化數(shù)據(jù)

vue 組件初始化數(shù)據(jù)

一共三層 App.vue -> index.vue -> header.vue
希望頁面初始化 子組件header.vue能拿到父組件傳下來的數(shù)據(jù)。我嘗試過直接傳值沒問題比如傳個字符串啥的,但是我想通過先ajax請求拿到j(luò)son對象數(shù)據(jù)后再傳到header,一直為空對象。剛開始用,求解答

父組件
<template>

<dbCarousel :data="data_carousel"></dbCarousel>

<script>

data(){
      return {
        data_carousel:{}
      }
},
created(){
       var _this = this;
      $.get('...',function(d){
          _this.data_carousel = d.carousel;
          //這一步并沒有起作用,data_carousel并沒有修改
          console.log(_this.data_carousel); //這里打印出來沒問題的
      })
}

子組件
<script>

props:[
      "data"
],
created(){
      console.log(this.data);
}
回答
編輯回答
孤慣

子組件

props: ["data"],
watch: {
    data: { 
        deep: true,
        handler () { // 每次data數(shù)據(jù)變動的時候執(zhí)行
            console.log(this.data)
        }
    }
},
created () { // 在組件初始化的時候執(zhí)行,只執(zhí)行一次
    console.log(this.data) 
}
2018年6月4日 18:17