分享

Vue.js——60分钟组件快速入门(上篇)

 昵称30961385 2016-12-28

组件简介

组件系统是Vue.js其中一个重要的概念,它提供了一种抽象,让我们可以使用独立可复用的小组件来构建大型应用,任意类型的应用界面都可以抽象为一个组件树:

那么什么是组件呢?
组件可以扩展HTML元素,封装可重用的HTML代码,我们可以将组件看作自定义的HTML元素。

本文的Demo和源代码已放到GitHub,如果您觉得本篇内容不错,请点个赞,或在GitHub上加个星星!
(所有示例都放在GitHub Pages上了,请访问https://github.com/keepfool/vue-tutorials查看示例汇总)

由于组件的篇幅较大,我将会把组件的入门知识分为两篇来讲解,这样也便于各位看官们快速消化。

组件的创建和注册

基本步骤

Vue.js的组件的使用有3个步骤:创建组件构造器、注册组件和使用组件。

image

下面的代码演示了这3个步骤:

html> body> div id='app'> my-component>my-component> div> body> script src='js/vue.js'>script> script> // 1.创建一个组件构造器 var myComponent = Vue.extend({ template: '
This is my first component!
'
}) // 2.注册组件,并指定组件的标签,组件的HTML标签为 Vue.component('my-component', myComponent) new Vue({ el: '#app' });
script>html>

运行结果如下:

image

可以看到,使用组件和使用普通的HTML元素没什么区别。

理解组件的创建和注册

我们用以下几个步骤来理解组件的创建和注册:

1. Vue.extend()是Vue构造器的扩展,调用Vue.extend()创建的是一个组件构造器。
2. Vue.extend()构造器有一个选项对象,选项对象的template属性用于定义组件要渲染的HTML。
3. 使用Vue.component()注册组件时,需要提供2个参数,第1个参数时组件的标签,第2个参数是组件构造器。
4. 组件应该挂载到某个Vue实例下,否则它不会生效。

请注意第4点,以下代码在3个地方使用了标签,但只有#app1和#app2下的标签才起到作用。

html> body> div id='app1'> my-component>my-component> div> div id='app2'> my-component>my-component> div> my-component>my-component> body> script src='js/vue.js'>script> script> var myComponent = Vue.extend({ template: '
This is a component!
'
}) Vue.component('my-component', myComponent) var app1 = new Vue({ el: '#app1' }); var app2 = new Vue({ el: '#app2' })
script>html>

image

View Demo

全局注册和局部注册

调用Vue.component()注册组件时,组件的注册是全局的,这意味着该组件可以在任意Vue示例下使用。
如果不需要全局注册,或者是让组件使用在其它组件内,可以用选项对象的components属性实现局部注册

上面的示例可以改为局部注册的方式:

html> body> div id='app'> my-component>my-component> div> body> script src='js/vue.js'>script> script> // 1.创建一个组件构造器 var myComponent = Vue.extend({ template: '
This is my first component!
'
}) new Vue({ el: '#app', components: { // 2. 将myComponent组件注册到Vue实例下 'my-component' : myComponent } });
script>html>

由于my-component组件是注册在#app元素对应的Vue实例下的,所以它不能在其它Vue实例下使用。

div id='app2'> my-component>my-component>div>script> new Vue({ el: '#app2' });script>

如果你这样做了,浏览器会提示一个错误:

image

View Demo

父组件和子组件

我们可以在组件中定义并使用其他组件,这就构成了父子组件的关系。

html> body> div id='app'> parent-component> parent-component> div> body> script src='js/vue.js'>script> script> var Child = Vue.extend({ template: '

This is a child component!

'
}) var Parent = Vue.extend({ // 在Parent组件内使用标签 template :'

This is a Parent component

'
, components: { // 局部注册Child组件,该组件只能在Parent组件内使用 'child-component': Child } }) // 全局注册Parent组件 Vue.component('parent-component', Parent) new Vue({ el: '#app' })
script>html>

这段代码的运行结果如下:

image

我们分几个步骤来理解这段代码:

  1. var Child = Vue.extend(...)定义一了个Child组件构造器
  2. var Parent = Vue.extend(...)定义一个Parent组件构造器
  3. components: { 'child-component': Child },将Child组件注册到Parent组件,并将Child组件的标签设置为child-component
  4. template :'

    This is a Parent component

    child-component>'
    ,在Parent组件内以标签的形式使用Child组件。
  5. Vue.component('parent-component', Parent) 全局注册Parent组件
  6. 在页面中使用标签渲染Parent组件的内容,同时Child组件的内容也被渲染出来

image

Child组件是在Parent组件中注册的,它只能在Parent组件中使用,确切地说:子组件只能在父组件的template中使用。

请注意下面两种子组件的使用方式是错误的:

1. 以子标签的形式在父组件中使用

div id='app'> parent-component> child-component>child-component> parent-component>div>

为什么这种方式无效呢?因为当子组件注册到父组件时,Vue.js会编译好父组件的模板,模板的内容已经决定了父组件将要渲染的HTML。
相当于运行时,它的一些子标签只会被当作普通的HTML来执行,不是标准的HTML标签,会被浏览器直接忽视掉。

2. 在父组件标签外使用子组件

div id='app'> parent-component> parent-component> child-component> child-component>div>

运行这段代码,浏览器会提示以下错误

image

View Demo

组件注册语法糖

以上组件注册的方式有些繁琐,Vue.js为了简化这个过程,提供了注册语法糖。

使用Vue.component()直接创建和注册组件:

// 全局注册,my-component1是标签名称Vue.component('my-component1',{ template: '
This is the first component!
'
})var vm1 = new Vue({ el: '#app1'})

Vue.component()的第1个参数是标签名称,第2个参数是一个选项对象,使用选项对象的template属性定义组件模板。
使用这种方式,Vue在背后会自动地调用Vue.extend()

在选项对象的components属性中实现局部注册:

var vm2 = new Vue({ el: '#app2', components: { // 局部注册,my-component2是标签名称 'my-component2': { template: '
This is the second component!
'
}, // 局部注册,my-component3是标签名称 'my-component3': { template: '
This is the third component!
'
} }})

View Demo

使用script或template标签

尽管语法糖简化了组件注册,但在template选项中拼接HTML元素比较麻烦,这也导致了HTML和JavaScript的高耦合性。
庆幸的是,Vue.js提供了两种方式将定义在JavaScript中的HTML模板分离出来。

使用

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多