第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

vue中子組件的beforeCreate鉤子函數(shù)中讀取父組件傳入的數(shù)據(jù)出現(xiàn)報(bào)錯(cuò)

vue中子組件的beforeCreate鉤子函數(shù)中讀取父組件傳入的數(shù)據(jù)出現(xiàn)報(bào)錯(cuò)

動(dòng)漫人物 2019-03-20 17:19:28
<!DOCTYPE html><html><head>  <meta charset="UTF-8">  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">  <meta http-equiv="X-UA-Compatible" content="ie=edge">  <meta name="renderer" content="webkit">  <title>父子組件生命周期</title></head><body>  <div id="app">    <p>{{parentData}}</p>    <child message="hello子組件"></child>  </div>  <script src="https://cdn.bootcss.com/vue/2.5.15/vue.js"></script>  <script>    var app = new Vue({      el: '#app',      data: {        parentData: '父組件數(shù)據(jù)',      },      beforeCreate() {        console.log(`Parent--beforeCreate ${this.parentData} ${this.$el}`);      },      created() {        console.log(`Parent--created ${this.parentData} ${this.$el}`);      },      components: {        child: {          template: `<div><p>{{message}}</p> <p>{{childrenData}}</p></div>`,          props: {            message: {              type: String            }          },          data: function () {            return {              childrenData: '子組件數(shù)據(jù)'            }          },          beforeCreate() {            console.log(this);            console.log(`Child--beforeCreate ${this.message} ${this.childrenData} ${this.$el}`);          },          created() {            console.log(`Child--created ${this.message} ${this.childrenData} ${this.$el}`);          },        }      }    })  </script></body>下面是輸出子組件在beforeCreate中讀取外部的傳入的變量時(shí)報(bào)錯(cuò)了,但是令我疑惑的是,上面可以輸出this的值,如果message沒有綁定到子組件上,最多就是輸出undefined值,而不應(yīng)該報(bào)錯(cuò)。難道是vue在內(nèi)部規(guī)定了子組件在beforeCreate的鉤子函數(shù)中不能讀取外部傳入的數(shù)據(jù),否則報(bào)錯(cuò)?
查看完整描述

2 回答

?
陪伴而非守候

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超8個(gè)贊

因?yàn)関ue的數(shù)據(jù)劫持機(jī)制

在beforeCreate鉤子中訪問 this.message 實(shí)際上調(diào)用了

this.message的get函數(shù) 而在get函數(shù)中vue返回的是 this['_props']['message']

顯然這個(gè)時(shí)候 this['_props'] 為undefined 這就是報(bào)錯(cuò)原因.


查看完整回答
反對(duì) 回復(fù) 2019-04-09
  • 2 回答
  • 0 關(guān)注
  • 1698 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)