云驹博客

路漫漫其修远兮,吾将上下而求索。

0%

Vue组件入门,Vue.component

基本示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<div id="box">
<button-counter></button-counter>
</div>

<script>
// 定义一个名为 button-counter 的新组件
Vue.component("button-counter", {
data: function () {
return {
count: 0,
};
},
template:
'<button v-on:click="count++">You clicked me {{ count }} times.</button>',
});

new Vue({
el: "#box",
});
</script>

data 必须是一个函数

当我们定义组件时,你可能会发现它的 data 并不是像这样直接提供一个对象:

1
2
3
data: {
count: 0;
}

取而代之的是,一个组件的 data 选项必须是一个函数,因此每个实例可以维护一份被返回对象的独立的拷贝:

1
2
3
4
5
data: function () {
return {
count: 0
}
}

通过 Prop 向子组件传递数据

基本示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<div id="#blog-post-demo">
<blog-post title="你好"></blog-post>
<blog-post title="hello"></blog-post>
</div>

<script>
Vue.component("blog-post", {
props: ["title"],
template: "<h3>{{ title }} vue</h3>",
});

new Vue({
el: "#blog-post-demo",
});
</script>

<!-- 结果 -->
// 你好 vue // hello vue

然而在一个典型的应用中,你可能在 data 里有一个博文的数组:

1
2
3
4
5
6
7
8
9
10
new Vue({
el: "#blog-post-demo",
data: {
posts: [
{ id: 1, title: "My journey with Vue" },
{ id: 2, title: "Blogging with Vue" },
{ id: 3, title: "Why Vue is so fun" },
],
},
});

并想要为每篇博文渲染一个组件就可以这么做:

1
2
3
4
5
<blog-post
v-for="post in posts"
v-bind:key="post.id"
v-bind:title="post.title"
></blog-post>

监听子组件事件

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<div id="#blog-posts-events-demo">
<blog-post v-on:enlarge-text="postFontSize += 0.1"></blog-post>
</div>

<script>
Vue.component("blog-post", {
template: `
<div class="blog-post">
<h3>{{ post.title }}</h3>
<button v-on:click="$emit('enlarge-text')">
Enlarge text
</button>
<div v-html="post.content"></div>
</div>
`,
});

new Vue({
el: "#blog-posts-events-demo",
data: {
postFontSize: 1,
},
});
</script>

监听子组件事件

使用事件抛出一个值

有的时候用一个事件来抛出一个特定的值是非常有用的。例如我们可能想让 <blog-post> 组件决定它的文本要放大多少。这时可以使用 $emit 的第二个参数来提供这个值:

1
<button v-on:click="$emit('enlarge-text', 0.1)">Enlarge text</button>

然后当在父级组件监听这个事件的时候,我们可以通过 $event 访问到被抛出的这个值:

1
<blog-post ... v-on:enlarge-text="postFontSize += $event"></blog-post>

或者,如果这个事件处理函数是一个方法:

1
<blog-post ... v-on:enlarge-text="onEnlargeText"></blog-post>

那么这个值将会作为第一个参数传入这个方法:

1
2
3
4
5
methods: {
onEnlargeText: function (enlargeAmount) {
this.postFontSize += enlargeAmount
}
}

使用事件抛出一个值

在组件上使用 v-model

自定义事件也可以用于创建支持 v-model 的自定义输入组件。记住:

1
<input v-model="searchText" />

等价于:

1
2
3
4
<input
v-bind:value="searchText"
v-on:input="searchText = $event.target.value"
/>

当用在组件上时,v-model 则会这样:

1
2
3
4
<custom-input
v-bind:value="searchText"
v-on:input="searchText = $event"
></custom-input>

为了让它正常工作,这个组件内的 <input> 必须:

  • 将其 value 特性绑定到一个名叫 value 的 prop 上
  • 在其 input 事件被触发时,将新的值通过自定义的 input 事件抛出

写成代码之后是这样的:

1
2
3
4
5
6
7
8
9
Vue.component("custom-input", {
props: ["value"],
template: `
<input
v-bind:value="value"
v-on:input="$emit('input', $event.target.value)"
>
`,
});

现在 v-model 就应该可以在这个组件上完美地工作起来了:

1
<custom-input v-model="searchText"></custom-input>

在组件上使用 v-model

通过插槽分发内容

和 HTML 元素一样,我们经常需要向一个组件传递内容,像这样:

1
<alert-box> Something bad happened. </alert-box>

使用 Vue 自定义的 slot 元素让这变得非常简单:

1
2
3
4
5
6
7
8
Vue.component("alert-box", {
template: `
<div class="demo-alert-box">
<strong>Error!</strong>
<slot></slot>
</div>
`,
});

通过插槽分发内容

动态组件

有的时候,在不同组件之间进行动态切换是非常有用的,比如在一个多标签的界面里。

通过 Vue 的<component元素加一个特殊的 is 特性来实现:

1
2
<!-- 组件会在 `currentTabComponent` 改变时改变 -->
<component v-bind:is="currentTabComponent"></component>

在上述示例中,currentTabComponent 可以包括

  • 已注册组件的名字,或
  • 一个组件的选项对象

动态组件

注意事项

  • 上述的这个和一些接下来的示例使用了 JavaScript 的模板字符串来让多行的模板更易读。它们在 IE 下并没有被支持,所以如果你需要在不 (经过 Babel 或 TypeScript 之类的工具) 编译的情况下支持 IE,请使用折行转义字符取而代之。

  • 解析 DOM 模板时的注意事项

    有些 HTML 元素,诸如 <ul><ol><table><select>,对于哪些元素可以出现在其内部是有严格限制的。而有些元素,诸如 <li><tr><option>,只能出现在其它某些特定的元素内部。

    这会导致我们使用这些有约束条件的元素时遇到一些问题。例如:

    1
    2
    3
    <table>
    <blog-post-row></blog-post-row>
    </table>

    这个自定义组件 <blog-post-row> 会被作为无效的内容提升到外部,并导致最终渲染结果出错。幸好这个特殊的 is 特性给了我们一个变通的办法:

    1
    2
    3
    <table>
    <tr is="blog-post-row"></tr>
    </table>

    需要注意的是如果我们从以下来源使用模板的话,这条限制是*不存在*的