1.$props
<body>
<div id="app">
<father></father>
</div>
<script src="js/vue.js"></script>
<script>
Vue.component('father',{
template:`
<div>
<h3>手机信息搜索</h3>
手机品牌:<input type="text" v-model="brand">
<child :name="brand"></child>
</div>
`,
data(){
return{
brand:''
}
}
})
Vue.component('child',{
props:['name'],
template:`
<ul>
<li>手机品牌:{{show.brand}}</li>
<li>手机型号:{{show.type}}</li>
<li>手机价格:{{show.price}}</li>
</ul>
`,
data(){
return{
content:[
{brand:'华为',type:"Mate20",price:3299},
{brand:'小米',type:"Mate20",price:3299},
{brand:'苹果',type:"Mate20",price:3299},
{brand:'三星',type:"Mate20",price:3299},
{brand:'魅族',type:"Mate20",price:3299},
{brand:'vivo',type:"Mate20",price:3299},
{brand:'oppo',type:"Mate20",price:3299}
],
show:{brand:'',type:'',price:""}
}
},
watch:{
name(){
if(this.$props.name){
var found=false;
this.content.forEach((value,index) => {
if(value.brand===this.$props.name){
found=value;
}
})
this.show=found?found:{brand:'',type:'',price:""}
}
}
}
})
new Vue({
el:"#app"
})
</script>
</body>
2.$options:获取的是vue实例中的选项,包含自定义选项
<body>
<div id='app'>
<button @click="chk">查看</button>
<h1>{{base}}</h1>
<h3>{{noBase}}</h3>
</div>
<script src="js/vue.js"></script>
<script>
new Vue({
el:"#app",
customOption:'我是自定义选项',
data:{
base:"我是基础数据"
},
methods:{
chk(){
console.log(this.$options);
console.log(this.$options.el);
}
},
created(){
this.noBase=this.$options.customOption
}
})
</script>
</body>
3.$el:访问vue实例的根DOM元素
<body>
<div id="app"></div>
<script src="js/vue.js"></script>
<script>
var vm= new Vue({
el:"#app"
})
console.log(vm.$el);
vm.$el.innerHTML='<div>我是被替换的div标签</div>'
</script>
</body>
4.$root获取根vue实例
<body>
<div id="app">
<my-component></my-component>
</div>
<script src="js/vue.js"></script>
<script>
Vue.component('my-component',{
template:`
<div>
<button @click="chk">查看根vue实例</button>
</div>
`,
methods:{
chk(){
console.log(this.$root);
console.log(this.$root===vm.$root);
}
}
})
var vm= new Vue({
el:"#app"
})
</script>
</body>