An error occurred when reading the incoming data from the parent component in the beforeCreate hook function of the vue sub-component

<!DOCTYPE html>
<html lang="zh-CN">
<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">
    

{{parentData}}

<child message="hello"></child> </div> <script src="https://cdn.bootcss.com/vue/2.5.15/vue.js"></script> <script> var app = new Vue({ el: "-sharpapp", data: { parentData: "", }, beforeCreate() { console.log(`Parent--beforeCreate ${this.parentData} ${this.$el}`); }, created() { console.log(`Parent--created ${this.parentData} ${this.$el}`); }, components: { child: { template: `<div>

{{message}}

{{childrenData}}

</div>`, props: { message: { type: String } }, data: function () { return { childrenData: "" } }, 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>

the following is the output

subcomponent reads external incoming variables in beforeCreate Times is wrong, but what puzzles me is that the above can output the value of this, if the message is not bound to the subcomponent, the most is to output undefined value, and should not report an error.
does vue internally specify that subcomponents cannot read externally incoming data in the hook function of beforeCreate, otherwise an error will be reported?

Mar.23,2021

vue
beforeCreate this.message 
this.messageget getvue this['_props']['message']
 this['_props'] undefined .


indeed, normally speaking, when this is object, this.message will never report an error.

but the goose js will not deceive you, so the pot will have to be carried by vue.

you can click on the line where the error is reported in the console to see the vue code that threw the error, that is, vue.esm.js?a026:3297:
at VueComponent.proxyGetter [as trigger] (vue.esm.js?a026:3297)

then you will find that the code looks something like this:

var sharedPropertyDefinition = {
  enumerable: true,
  configurable: true,
  get: noop,
  set: noop
};

function proxy (target, sourceKey, key) {
  sharedPropertyDefinition.get = function proxyGetter () {
    return this[sourceKey][key] //  sourceKey_props, keymessage
  };
  sharedPropertyDefinition.set = function proxySetter (val) {
    this[sourceKey][key] = val;
  };
  Object.defineProperty(target, key, sharedPropertyDefinition);
}

focuses on return this [sourceKey] [key] .

actually this.message calls not the message property under the vue instance object, but this._props.message .
and this._props at this time (beforeCreate) is undefined .
this is related to the life cycle of vue. You can see this figure: https://cn.vuejs.org/images/l..

And, if you console.log (this) in the created hook, you will find the message attribute under this . (there is also _ props.message).

because there is a passage in the code posted above: Object.defineProperty (target, key, sharedPropertyDefinition);
target is the current vue instance, and key is a props such as message.
(vue uses defineProperty to point this.message to this._props.message .)

so, we can do another experiment:

  created() {
    console.log(this._props) // object
    console.log(this.message, 11)  // 'message content'
    delete this._props.message
    console.log(this.message, 22) // undefined
  }
Menu