<!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?