1.获取子组件的内容为什么输出undefined呀
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript" src="js/vue.js">
</script>
</head>
<body>
<div id="app">
<button type="button" @click="a">点我然后打开控制台</button>
<div id="app1" ref="myid1">
{{msg1}}
</div>
<aaa :msg2='msg2' ref="mytem1"></aaa>
<button type="button" @click="b">点我然后打开控制台</button>
</div>
<script type="text/javascript">
var vm=new Vue({
data:{
msg1:"hello,this is Dom",
msg2:"this is a template and data form Daddy",
},
methods:{
a:function(){
var idl=this.$refs.myid1.innerHTML;
console.log(idl);
},
b:function(){
var idk=this.$refs.mytem1.innerHTML;
console.log(idk);
},
},
components:{
'aaa':{
template:`
<div>
{{msg2}}
</div>`,
props:['msg2'],
},
},
}).$mount("#app");
</script>
</body>
</html>
应该是
this.$refs.mytem1.innerHTML
取不到值吧。原因:
ref="mytem1"
是挂载在组件aaa
上,所以this.$refs.mytem1
指向的是一个组件,而不是dom,所以没有innerHTML
属性。