# 处理边界情况
一些特殊的情况的写法 vue 中以$开头的是代表 vue 实例中所具有的属性或方法 访问元素 & 组件
# 访问根元素
访问根元素中的属性或方法 this.$root
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<script src="05-vue/lib/vue.js"></script>
<style>
.on {
color: red;
}
</style>
</head>
<body>
<div id="app">
<com1></com1>
</div>
<script>
var vm = new Vue({
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() {
console.log(this.$root.alerts());
},
},
},
},
},
});
</script>
</body>
</html>
# 访问父元素
访问父元素的属性或方法 this.$parent
# 访问子组件
访问子组件实例或子元素 this.$ref
# X-Template
<script type="text/x-template" id="hello-world-template">
<p>Hello hello hello</p>
</script>
<script>
Vue.component("hello-world", {
template: "#hello-world-template",
});
</script>