1.在html5的规范里,要求tbody里必须写tr,导致浏览器解析row的时候出问题,此时可借助is属性来解决,ul li select option同理
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>VUE中组件使用的细节点</title>
</head>
<body>
<div id="app">
<table>
<tbody>
<row></row>
<row></row>
<row></row>
</tbody>
</table>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
Vue.component('row',{
template: '<tr><td>this is a row</td></tr>'
});
var app = new Vue({
el: '#app'
});
</script>
</html>
2.在子组件中定义data时,data必须是个函数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>VUE中组件使用的细节点</title>
</head>
<body>
<div id="app">
<table>
<tbody>
<tr is="row"></tr>
<tr is="row"></tr>
<tr is="row"></tr>
</tbody>
</table>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
Vue.component('row',{
data: function () {
return {
content: 'this is content'
}
},
template: '<tr><td>{{content}}</td></tr>'
});
var app = new Vue({
el: '#app'
});
</script>
</html>
3.Vue中操作DOM,当@ref在标签上时,获取到的就是dom元素,在组件上时获取到的就是组件对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>VUE中组件使用的细节点</title>
</head>
<body>
<div id="app">
<div ref="helloDiv">hello</div>
<counter ref="counter1" @change="handleChange"></counter>
<counter ref="counter2" @change="handleChange"></counter>
<div>{{total}}</div>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
Vue.component('counter',{
template: '<div @click="handleClick">{{number}}</div>',
data: function () {
return {
number: 0
}
},
methods: {
handleClick: function (){
this.number++;
this.$emit('change');
}
}
});
var app = new Vue({
el: '#app',
methods: {
handleChange: function () {
console.log(this.$refs.helloDiv.innerHTML);
this.total = this.$refs.counter1.number + this.$refs.counter2.number;
}
},
data: {
total: 0
}
});
</script>
</html>