zoukankan      html  css  js  c++  java
  • vue 学习记录

    模板:通常是指html模板

    组件component的概念:

    在 Vue 里,一个组件本质上是一个拥有预定义选项的一个 Vue 实例,

    将组件看作自定义的HTML元素。使用组件的前提是创建并注册组件

     vue组件介绍:http://blog.csdn.net/xingjigongsi/article/details/54602404

    Vue.js的组件的使用有3个步骤:

    1:创建组件构造器------- 调用Vue.extend({template:''})方法创建组件构造器         Vue.extend(html 模板)

    2:注册组件------调用Vue.component()方法,注册组件

    3:使用组件------------在Vue实例的作用范围内使用组件

    例如:

     1 <!DOCTYPE html>
     2 <html>
     3     <body>
     4         <div id="app">
     5             <!-- 3. #app是Vue实例挂载的元素,应该在挂载元素范围内使用组件-->
     6             <my-component></my-component>
     7         </div>
     8     </body>
     9     <script src="js/vue.js"></script>
    10     <script>
    11     
    12         // 1.创建一个组件构造器
    13         var myComponent = Vue.extend({
    14             template: '<div>This is my first component!</div>'
    15         })
    16         
    17         // 2.注册组件,并指定组件的html标签,组件的HTML标签为<my-component>
    18         Vue.component('my-component', myComponent)
    19         
    20         new Vue({
    21             el: '#app'
    22         });
    23         
    24     </script>
    25 </html>

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

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

      如 id为app的vue实例下

       new Vue({
                 el: '#app'
               });

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

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

    三:父组件和子组件

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

     1 <!DOCTYPE html>
     2 <html>
     3     <body>
     4         <div id="app">
     5             <parent-component>
     6             </parent-component>
     7         </div>
     8     </body>
     9     <script src="js/vue.js"></script>
    10     <script>
    11         
    12         var Child = Vue.extend({
    13             template: '<p>This is a child component!</p>'
    14         })
    15         
    16         var Parent = Vue.extend({
    17             // 在Parent组件内使用<child-component>标签
    18             template :'<p>This is a Parent component</p><child-component></child-component>',
    19             components: {
    20                 // 局部注册Child组件,该组件只能在Parent组件内使用
    21                 'child-component': Child
    22             }
    23         })
    24         
    25         // 全局注册Parent组件
    26         Vue.component('parent-component', Parent)
    27         
    28         new Vue({
    29             el: '#app'
    30         })
    31         
    32     </script>
    33 </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组件的内容也被渲染出来

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

    <div id="app">
        <parent-component>
            <child-component></child-component>
        </parent-component>
    </div>  错误
    <div id="app">
        <parent-component>
        </parent-component>
        <child-component>
        </child-component>
    </div>  错误                      确切地说:子组件只能在父组件的template中使用。 

    三:组件注册语法糖

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

    // 全局注册,my-component1是标签名称
    Vue.component('my-component1',{
        template: '<div>This is the first component!</div>'
    })
    
    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: '<div>This is the second component!</div>'
            },
            // 局部注册,my-component3是标签名称
            'my-component3': {
                template: '<div>This is the third component!</div>'
            }
        }
    })

    四:使用script或template标签

    template选项现在不再是HTML元素,而是一个idVue.js根据这个id查找对应的元素,然后将这个元素内的HTML作为模板进行编译。

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
        <body>
            <div id="app">
                <my-component></my-component>
            </div>
            
    注意:使用<script>标签时,type指定为text/x-template,意在告诉浏览器这不是一段js脚本,浏览器在解析HTML文档时会忽略<script>标签内定义的内容。
         <script type="text/x-template" id="myComponent">
                <div>This is a component!</div>
            </script>
            <template id="myComponent">
                <div>This is a component!</div>
            </template>
        </body>
        <script src="js/vue.js"></script>
        <script>
            
            Vue.component('my-component',{
                template: '#myComponent'
            })
            
            new Vue({
                el: '#app'
            })
            
        </script>
    </html>

    这使得HTML代码和JavaScript代码是分离的,便于阅读和维护。
    另外,在Vue.js中,可创建.vue后缀的文件,在.vue文件中定义组件。

    五:组件的el和data选项

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

    Vue.component('my-component', {
        data: {
            a: 1
        }
    }) 报错,在定义组件的选项时,data和el选项必须使用函数。

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

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

    六:使用props

    组件实例的作用域是孤立的。这意味着不能并且不应该在子组件的模板内直接引用父组件的数据。可以使用 props 把数据传给子组件。

    组件的props选项,它用于将父组件的数据传递给子组件,prop 是父组件用来传递数据的一个自定义属性

    注意:在子组件中定义prop时,使用了camelCase命名法。由于HTML特性不区分大小写,camelCase的prop用于特性时,需要转为 kebab-case(短横线隔开)。例如,在prop中定义的myName,在用作特性时需要转换为my-name。

    js中用驼峰式写法,html中因为不区分大小写,需要用短线-隔开。

    var vm = new Vue({
        el: '#app',
        data: {
            name: 'keepfool',
            age: 28
        },
        components: {
            'my-component': {
                template: '#myComponent',
                props: ['myName', 'myAge']
            }
        }
    })
    为了便于理解,你可以将这个Vue实例看作my-component的父组件。
    如果我们想使父组件的数据,则必须先在子组件中定义props属性,也就是props: ['myName', 'myAge']这行代码。
    
    定义子组件的HTML模板:
    <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>

    prop的绑定类型

    单向绑定

    组件模板:是带有组件的html模板

    总结:

    Vue.component用来注册组件,第一个参数是组件名称,第二个参数是组成组件的对象,即html模板template

    new Vue中的components参数可以局部注册多个组件,{Vue.component1, Vue.component2,...}

  • 相关阅读:
    C#学习之委托和事件
    ArcGIS许可启动问题
    空间插值——克里金插值
    maven 问题解决 tools以及jconsole两个jar包 无效
    JDBC代码示例
    mysql 同一IP 产生太多终端的数据库连接导致阻塞
    apache 反向代理配置
    oracle、mysql、sql server等;流行数据库的链接驱动配置
    POI XSSF与HSSF的 使用区别
    使用IDEA开发Activiti工作流
  • 原文地址:https://www.cnblogs.com/Joans/p/6993610.html
Copyright © 2011-2022 走看看