组件注册
组件名
在注册一个组件的时候,我们始终需要给它一个名字。比如在全局注册的时候我们已经看到了:
| Vue.component('my-component-name', { /* ... */ })
|
该组件名就是 Vue.component
的第一个参数。
你给予组件的名字可能依赖于你打算拿它来做什么。
当直接在 DOM 中使用一个组件 (而不是在字符串模板或单文件组件) 的时候,我们强烈推荐遵循 W3C 规范中的自定义组件名 (字母全小写且必须包含一个连字符)。
这会帮助你避免和当前以及未来的 HTML 元素相冲突。
组件名大小写
定义组件名的方式有两种:
使用 kebab-case
| Vue.component('my-component-name', { /* ... */ })
|
当使用 kebab-case (短横线分隔命名) 定义一个组件时,你也必须在引用这个自定义元素时使用 kebab-case,例如 <my-component-name>
。
使用 PascalCase
| Vue.component('MyComponentName', { /* ... */ })
|
当使用 PascalCase (首字母大写命名) 定义一个组件时,你在引用这个自定义元素时两种命名法都可以使用。也就是说 <my-component-name>
和 <MyComponentName>
都是可接受的。注意,尽管如此,直接在 DOM (即非字符串的模板) 中使用时只有 kebab-case 是有效的。
全局注册
用 Vue.component
来创建组件:
| Vue.component('my-component-name', {
// ... 选项 ...
})
|
这些组件是全局注册的。也就是说它们在注册之后可以用在任何新创建的 Vue 根实例 (new Vue
) 的模板中。比如:
| Vue.component('component-a', { /* ... */ })
Vue.component('component-b', { /* ... */ })
Vue.component('component-c', { /* ... */ })
new Vue({ el: '#app' })
|
| <div id="app">
<component-a></component-a>
<component-b></component-b>
<component-c></component-c>
</div>
|
在所有子组件中也是如此,也就是说这三个组件在各自内部也都可以相互使用。
使用 is
标签解决 bug
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | <div id="root">
<table>
<tbody>
<row></row>
<row></row>
<row></row>
</tbody>
</table>
</div>
<script>
Vue.component('row', {
template: "<tr><td>this is a row</td></tr>"
})
var vm = new Vue({
el: "#root",
})
</script>
|
html5 要求 tbody
里面必须有 tr
,所以会出现问题,如下图
解决方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | <div id="root">
<table>
<tbody>
<tr is="row"></tr>
<tr is="row"></tr>
<tr is="row"></tr>
</tbody>
</table>
</div>
<script>
Vue.component('row', {
template: "<tr><td>this is a row</td></tr>"
})
var vm = new Vue({
el: "#root",
})
</script>
|
子组件中的 data 必须是一个函数,不能是一个对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 | <div id="root">
<table>
<tbody>
<tr is="row"></tr>
<tr is="row"></tr>
<tr is="row"></tr>
</tbody>
</table>
</div>
<script>
Vue.component('row', {
data: function() {
return {
content: 'this is content'
}
},
template: "<tr><td>{{ content }}</td></tr>"
})
var vm = new Vue({
el: "#root",
})
</script>
|
ref引用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | <div id="root">
<div
ref='hello'
@click="handleClick"
>
hello world
</div>
</div>
<script>
var vm = new Vue({
el: "#root",
methods: {
handleClick: function() {
alert(this.$refs.hello.innerHTML)
}
}
})
</script>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 | <div id="root">
<counter ref="one" @change="handleChange"></counter>
<counter ref="two" @change="handleChange"></counter>
<div>{{ total }}</div>
</div>
<script>
Vue.component('counter', {
template: '<div @click="handleClick">{{ number }}</div>',
data: function() {
return {
number: 0
}
},
methods: {
handleClick: function() {
this.number++,
this.$emit('change')
}
}
})
var vm = new Vue({
el: "#root",
data: {
total: 0
},
methods: {
handleChange: function() {
this.total = this.$refs.one.number + this.$refs.two.number
}
}
})
</script>
|
局部注册
全局注册往往是不够理想的。比如,如果你使用一个像 webpack 这样的构建系统,全局注册所有的组件意味着即便你已经不再使用一个组件了,它仍然会被包含在你最终的构建结果中。这造成了用户下载的 JavaScript 的无谓的增加。
在这些情况下,你可以通过一个普通的 JavaScript 对象来定义组件:
| var ComponentA = { /* ... */ }
var ComponentB = { /* ... */ }
var ComponentC = { /* ... */ }
|
然后在 components
选项中定义你想要使用的组件:
| new Vue({
el: '#app',
components: {
'component-a': ComponentA,
'component-b': ComponentB
}
})
|
对于 components
对象中的每个属性来说,其属性名就是自定义元素的名字,其属性值就是这个组件的选项对象。
注意局部注册的组件在其子组件中不可用。例如,如果你希望 ComponentA 在 ComponentB 中可用,则你需要这样写:
| var ComponentA = { /* ... */ }
var ComponentB = {
components: {
'component-a': ComponentA
},
// ...
}
|