vue自定义组件

组件的建立和注册

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

<!DOCTYPE html>
<html>
    <body>
        <div id="app">
            <!-- 3. #app是Vue实例挂载的元素,应该在挂载元素范围内使用组件-->
            <my-component></my-component>
        </div>
    </body>
    <script src="js/vue.js"></script>
    <script>
    
        // 1.建立一个组件构造器
        var myComponent = Vue.extend({
            template: '<div>This is my first component!</div>'
        })
        
        // 2.注册组件,并指定组件的标签,组件的HTML标签为<my-component>
        Vue.component('my-component', myComponent)
        
        new Vue({
            el: '#app'
        });
        
    </script>
</html>

理解组件的建立和注册

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

 

全局注册和局部注册

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

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

父组件和子组件

<!DOCTYPE html>
<html>
    <body>
        <div id="app">
            <parent-component>
            </parent-component>
        </div>
    </body>
    <script src="js/vue.js"></script>
    <script>
        var Child = Vue.extend({
            template: '<p>This is a child component!</p>'
        })
        
        var Parent = Vue.extend({
            // 在Parent组件内使用<child-component>标签
            template :'<p>This is a Parent component</p><child-component></child-component>',
            components: {
                // 局部注册Child组件,该组件只能在Parent组件内使用
                'child-component': Child
            }
        })
        
        // 全局注册Parent组件
        Vue.component('parent-component', Parent)
        
        new Vue({
            el: '#app'
        })
        
    </script>
</html>
  1. var Child = Vue.extend(...)定义一了个Child组件构造器
  2. var Parent = Vue.extend(...)定义一个Parent组件构造器
  3. components: { 'child-component': Child },将Child组件注册到Parent组件,并将Child组件的标签设置为child-component
  4. template :'<p>This is a Parent component</p><child-component></child-component>',在Parent组件内以标签的形式使用Child组件。
  5. Vue.component('parent-component', Parent) 全局注册Parent组件
  6. 在页面中使用<parent-component>标签渲染Parent组件的内容,同时Child组件的内容也被渲染出来

组件的el和data选项

传入Vue构造器的多数选项也能够用在 Vue.extend() 或Vue.component()中,不过有两个特例: data 和el
Vue.js规定:在定义组件的选项时,data和el选项必须使用函数。函数

使用props

组件实例的做用域是孤立的。这意味着不能而且不该该在子组件的模板内直接引用父组件的数据。能够使用 props 把数据传给子组件spa

props基础示例

var vm = new Vue({
    el: '#app',
    data: {
        name: 'keepfool',
        age: 28
    },
    components: {
        'my-component': {
            template: '#myComponent',
            props: ['myName', 'myAge']
        }
    }
})

定义子组件的HTML模板:3d

<template id="myComponent">
    <table>
        <tr>
            <th colspan="2">
                子组件数据
            </th>
        </tr>
        <tr>
            <td>my name</td>
            <td>{{ myName }}</td>
        </tr>
        <tr>
            <td>my age</td>
            <td>{{ myAge }}</td>
        </tr>
    </table>
</template>

将父组件数据经过已定义好的props属性传递给子组件:双向绑定

<div id="app">
    <my-component v-bind:my-name="name" v-bind:my-age="age"></my-component>
</div>

在父组件中使用子组件时,经过如下语法将数据传递给子组件:code

<child-component v-bind:子组件prop="父组件数据属性"></child-component>

prop的绑定类型

单向绑定

既然父组件将数据传递给了子组件,那么若是子组件修改了数据,对父组件是否会有所影响呢?component

prop默认是单向绑定:当父组件的属性变化时,将传导给子组件,可是反过来不会。这是为了防止子组件无心修改了父组件的状态

 

双向绑定

能够使用.sync显式地指定双向绑定,这使得子组件的数据修改会回传给父组件。htm

<my-component v-bind:my-name.sync="name" v-bind:my-age.sync="age"></my-component>

单次绑定

能够使用.once显式地指定单次绑定,单次绑定在创建以后不会同步以后的变化,这意味着即便父组件修改了数据,也不会传导给子组件。

<my-component v-bind:my-name.once="name" v-bind:my-age.once="age"></my-component>
相关文章
相关标签/搜索