vue.js初学入门教程(2)

4.组件使用基础

什么是组件?组件可以理解为可重用的自定义HTML。
可以使用一堆组件来构造大型应用,任意类型的应用界面都可以抽象为一个组件树:

vue.js初学入门教程(2)


可以把组件代码按照template、style、script的拆分方式,放置到对应的.vue文件中。
组件预定义选项中最核心的几个:

模板(template)、初始数据(data)、接受的外部参数(props)、方法(methods)、生命周期钩子函数(lifecycle hooks)。

4.1 基本步骤

使用组件首先需要创建构造器:

var MyComponent = Vue.extend({ // 选项... })

要把这个构造器用作组件,需要用 Vue.component(tag, constructor) 注册 :

// 全局注册组件,tag 为 my-component Vue.component('my-component', MyComponent)

然后使用:

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <div> <my-component></my-component> </div> </body> <script src="https://www.jb51.net/js/vue.js"></script> <script> var myComponent = Vue.extend({ template: '<p>9898不得了!</p>' }); Vue.component('my-component', myComponent); new Vue({ el: '#xxx' }); </script> </html>

其中,

vue.js初学入门教程(2)

Vue.component('my-component', MyComponent)这种是全局注册,第一个参数是注册组件的名称,第二个参数是组件的构造函数;

选项对象的template属性用于定义组件要渲染的HTML;

组件的模板替换了自定义元素,自定义元素的作用只是作为一个挂载点。组件挂载在vue实例上才会生效。

对于自定义标签名字,Vue.js 不强制要求遵循 W3C 规则(小写,并且包含一个短杠),为了避免不必要的事端尽管遵循这个规则。 

4.2 局部注册

用实例选项 components 注册:

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <div> <my-component></my-component> </div> </body> <script src="https://www.jb51.net/js/vue.js"></script> <script> var myComponent = Vue.extend({ template: '<p>9898不得了!</p>' }); // Vue.component('my-component', myComponent); new Vue({ el: '#xxx', components: { 'my-component': myComponent } }); </script> </html>

也可以在组件中定义并使用其他组件:

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <div> <xx-component></xx-component> </div> </body> <script src="https://www.jb51.net/js/vue.js"></script> <script> var Child = Vue.extend({ template: '<div>i am zai</div>', replace: true }) var Parent = Vue.extend({ template: '<p>i am baba</p><br/><wa></wa>', components: { // <xx-component>只能用在父组件模板内 'wa': Child } }) // 创建根实例 new Vue({ el: '#example', components: { 'xx-component': Parent } }) </script> </html>

vue.js初学入门教程(2)

其中,子组件只能在父组件的template中使用

另外,有简化的写法,Vue在背后会自动地调用Vue.extend():

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <div> <my-component-continue></my-component-continue> </div> </body> <script src="https://www.jb51.net/js/vue.js"></script> <script> // 局部注册的简化写法 var vm2 = new Vue({ el: '#xxx', components: { 'my-component': { template: '<div>9898不得了!</div>' }, 'my-component-continue': { template: '<div>粮食大丰收!</div>' } } }) </script> </html>

vue.js初学入门教程(2)

4.3 组件选项问题

在定义组件的选项时,data和el选项必须使用函数。

如果data选项指向某个对象,这意味着所有的组件实例共用一个data。
所以应当使用一个函数作为 data 选项,让这个函数返回一个新对象:

Vue.component('my-component', { data: function(){ return {a : 1} } })

同理,el 选项用在 Vue.extend() 中时也须是一个函数。 

5.数据传递

Vue.js组件之间有三种数据传递方式:

props
组件通信
slot

5.1 props

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzgwjs.html