<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><metahttp-equiv="X-UA-Compatible"content="ie=edge"><scriptsrc="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script><title>$root访问根组件中的属性或方法</title></head><body><divid="app"><com1></com1></div><script>var vm =newVue({el:"#app",data(){return{rootInfo:"我是根元素的属性"}},methods:{alerts(){alert(111)}},components:{com1:{data(){return{info:"组件1"}},template:"<p>{{ info }} <com2></com2></p>",components:{com2:{template:"<p>我是组件1的子组件</p>",created(){this.$root.alerts()//111
console.log(this.$root.rootInfo)//我是根元素的属性}}}}}});</script></body></html>