Vue---Vue组件

一.现实中的组件化思想体现

* 标准

* 分治

* 重用

* 组合

二.编程中的组件化思想体现

三.组件化开发思想

1.组件化规范:Web Components

* 我们希望尽可能多的重用代码

* 自定义组件的方式不太容易 (html、css、js)

* 多次使用组件可能会导致样式等冲突

Web Components通过创建封装好功能的定制元素解决上述问题

官网: 

Web Components | MDN

Vue部分实现了上述规范 

四.组件注册 

1.全局组件注册语法

 

案例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Document</title>
    <style>

    </style>
</head>

<body>
    <div id="app">
        <button-counter></button-counter>
    </div>
</body>
<script src="./js/vue.js"></script>
<script>
    // 组件注册
    Vue.component('button-counter', {
        data: function() {
            return {
                count: 0
            }
        },
        template: '<button @click="count++">点击了{{count}}次</button>'
    })
    let vm = new Vue({
        el: '#app',
        data: {}
    });
</script>

</html>

 案例改进:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Document</title>
    <style>

    </style>
</head>

<body>
    <div id="app">
        <button-counter></button-counter>
    </div>
</body>
<script src="./js/vue.js"></script>
<script>
    // 组件注册
    Vue.component('button-counter', {
        data: function() {
            return {
                count: 0
            }
        },
        template: '<button @click="handel">点击了{{count}}次</button>',
        methods: {
            handel: function() {
                this.count++
            }
        }
    })
    let vm = new Vue({
        el: '#app',
        data: {}
    });
</script>

</html>

2.组件用法

* 每个组件都是一个实例,各个组件之间互不影响数据相互独立 

 3.组件注册注意事项

①data必须是一个函数

②组件模板内容必须单个根元素

③组件模板内容可以是模板字符串(需要浏览器支持ES6语法

注意:

  • data必须是一个函数保证组件的数据都是互相独立的

  • 模板内容template必须是单个根元素(当模板中存在两个或多个元素时,需要在 最外层定义一个根元素div)

  • 组件模板使用模板字符串的作用是提高代码可读性,即代码可以换行书写

注意事项:

如果使用驼峰式命名组件,那么在使用组件的时候,只能在字符串模板中用驼峰的方式使用组件,但是在普通的标签模板中必须使用短横线的方式使用组件

案例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Document</title>
    <style>

    </style>
</head>

<body>
    <div id="app">
        <button-counter></button-counter>
        <hello-world></hello-world>
    </div>
</body>
<script src="./js/vue.js"></script>
<script>
    // 组件注册
    Vue.component('HelloWorld', {
        data: function() {
            return {
                msg: 'Hello World'
            }
        },
        template: '<div>{{msg}}</div>'

    })
    Vue.component('button-counter', {
        data: function() {
            return {
                count: 0
            }
        },
        template: `
        <div>
        <button @click="handel">点击了{{count}}次</button>
        <HelloWorld></HelloWorld>
        </div>`,
        methods: {
            handel: function() {
                this.count++
            }
        }
    })
    let vm = new Vue({
        el: '#app',
        data: {}
    });
</script>

</html>

 4.局部组件注册

注意:

 局部组件只能在注册他的父组件中使用

案例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Document</title>
    <style>

    </style>
</head>

<body>
    <div id="app">
        <hello-one></hello-one>
        <hello-two></hello-two>
        <hello-three></hello-three>
    </div>
</body>
<script src="./js/vue.js"></script>
<script>
    // 组件注册
    let HelloOne = {
        data: function() {
            return {
                msg: 'HelloOne'
            }
        },
        template: '<div>{{msg}}</div>'
    }
    let HelloTwo = {
        data: function() {
            return {
                msg: 'HelloTwo'
            }
        },
        template: '<div>{{msg}}</div>'
    }
    let HelloThree = {
        data: function() {
            return {
                msg: 'HelloThree'
            }
        },
        template: '<div>{{msg}}</div>'
    }
    let vm = new Vue({
        el: '#app',
        data: {},
        components: {
            'hello-one': HelloOne,
            'hello-two': HelloTwo,
            'hello-three': HelloThree

        }
    });
</script>

</html>

 五.组件间数据交互

1.父组件向子组件传值 

①子组件内部通过props接受传递过来的值 

 ②父组件通过属性将值传递给子组件

 *子组件内部通过props接受父组件的数据

案例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Document</title>
    <style>

    </style>
</head>

<body>
    <div id="app">
        <hello-one :msg="msg"></hello-one>
    </div>
</body>
<script src="./js/vue.js"></script>
<script>
    // 组件注册
    let HelloOne = {
        props: ['msg'],
        data: function() {
            return {}
        },
        template: '<div>从父组件接受的数据是:{{msg}}</div>'
    }
    let vm = new Vue({
        el: '#app',
        data: {
            msg: '这是父组件的数据'
        },
        components: {
            'hello-one': HelloOne
        }
    });
</script>

</html>

 ③props属性名规则

 *  在props中使用驼峰形式,模板中需要使用短横线的形式

*  字符串形式的模板中没有这个限制

*在html的属性中大小写是不敏感的,不区分的 

④props属性值类型 

* 字符串 String

* 数值 Number

* 布尔值 Boolean

* 数组 Array

* 对象 Object

案例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Document</title>
    <style>

    </style>
</head>

<body>
    <div id="app">
        <hello-one :str="str" :number="number" :boolean="boolean" :arr="arr" :obj="obj"></hello-one>
    </div>
</body>
<script src="./js/vue.js"></script>
<script>
    // 组件注册
    let HelloOne = {
        props: ['str', 'number', 'boolean', 'arr', 'obj'],
        data: function() {
            return {}
        },
        template: '<div>{{str}}{{number}}{{boolean}}{{arr[0]}}{{obj.name}}</div>'
    }
    let vm = new Vue({
        el: '#app',
        data: {
            str: '这是字符串',
            number: 0,
            boolean: true,
            arr: [0],
            obj: {
                name: '黄某某'
            }
        },
        components: {
            'hello-one': HelloOne
        }
    });
</script>

</html>

 2.子组件向父组件传值

①子组件通过自定义事件向父组件传递信息

 ②父组件监听子组件的事件

 *props传递数据原则:单项数据流(只能从父组件传递到子组件,而不能相反,如果子组件需要修改父组件的数据,则通过给父组件发消息让父组件自己修改

案例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Document</title>
    <style>

    </style>
</head>

<body>
    <div id="app">
        {{count}}
        <hello-one @add-count="count++"></hello-one>
    </div>
</body>
<script src="./js/vue.js"></script>
<script>
    // 组件注册
    let HelloOne = {
        data: function() {
            return {}
        },
        template: `<button @click="$emit('add-count')">增加父组件的count值</button>`
    }
    let vm = new Vue({
        el: '#app',
        data: {
            count: 0
        },
        components: {
            'hello-one': HelloOne
        }
    });
</script>

</html>

  3.子组件向父组件传值(带参数)

①子组件通过自定义事件向父组件传递信息 

②父组件监听子组件的事件 

 ($event为固定写法,接受子组件传递的参数)

案例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Document</title>
    <style>

    </style>
</head>

<body>
    <div id="app">
        {{count}}
        <hello-one @update-count="count=$event"></hello-one>
    </div>
</body>
<script src="./js/vue.js"></script>
<script>
    // 组件注册
    let HelloOne = {
        data: function() {
            return {}
        },
        template: `<button @click="$emit('update-count',3)">修改父组件的count值</button>`
    }
    let vm = new Vue({
        el: '#app',
        data: {
            count: 0
        },
        components: {
            'hello-one': HelloOne
        }
    });
</script>

</html>

 4.非父子组件传值

 案例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Document</title>
    <style>

    </style>
</head>

<body>
    <div id="app">
        <test-tom></test-tom>
        <test-jan></test-jan>
        <button @click="handel">点击销毁tom和jan的通信</button>
    </div>
</body>
<script src="./js/vue.js"></script>
<script>
    // 提供事件中心
    let hub = new Vue();
    Vue.component('test-tom', {
        data: function() {
            return {
                num: 0
            }
        },
        template: `
        <div>
        Tom:{{num}}
        <button @click="handel">点击添加Jan的数据</button>
        </div>
        `,
        methods: {
            //向Jan发消息通信,添加Jan的数据
            handel: function() {
                hub.$emit('jan-event', 2);
            }
        },
        mounted: function() {
            // 监听修改自己数据,通过val参数获取兄弟传过来的数据
            hub.$on('tom-event', (val) => {
                this.num = this.num + val
            })
        }
    })
    Vue.component('test-jan', {
        data: function() {
            return {
                num: 0
            }
        },
        template: `
        <div>
        Jan:{{num}}
        <button @click="handel">点击添加Tom的数据</button>
        </div>
        `,
        methods: {
            //向Tom发消息通信,添加Tom的数据
            handel: function() {
                hub.$emit('tom-event', 3);
            }
        },
        mounted: function() {
            // 监听修改自己数据,通过val参数获取兄弟传过来的数据
            hub.$on('jan-event', (val) => {
                this.num = this.num + val
            })
        }

    })

    let vm = new Vue({
        el: '#app',
        data: {
            count: 0
        },
        methods: {
            handel() {
                hub.$off('tom-event')
                hub.$off('jan-event')
            }
        }
    });
</script>

</html>

六.组件插槽

 1.组件插槽作用:父组件向子组件传递模板内容

 2.组件插槽的基本用法

①插槽位置

②插槽内容

 

 案例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Document</title>
    <style>

    </style>
</head>

<body>
    <div id="app">
        <alert-box>出错啦</alert-box>
    </div>
</body>
<script src="./js/vue.js"></script>
<script>
    Vue.component('alert-box', {
        data: function() {
            return {
                num: 0
            }
        },
        template: `
        <div>
        <strong>ERROR:</strong>
        <slot></slot>
        </div>
        `
    })

    let vm = new Vue({
        el: '#app',
        data: {}
    });
</script>

</html>

运行:

注意:当传递内容为空时,slot会显示默认内容,不为空时就替换slot的内容 

案例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Document</title>
    <style>

    </style>
</head>

<body>
    <div id="app">
        <alert-box>出错啦</alert-box>
        <alert-box></alert-box>
    </div>
</body>
<script src="./js/vue.js"></script>
<script>
    Vue.component('alert-box', {
        data: function() {
            return {
                num: 0
            }
        },
        template: `
        <div>
        <strong>ERROR:</strong>
        <slot>默认内容</slot>
        </div>
        `
    })

    let vm = new Vue({
        el: '#app',
        data: {}
    });
</script>

</html>

 运行结果:

3. 具名插槽用法

①插槽定义

 ②插槽内容

 

注意:没有匹配到的slot的标签会填充到默认的slot里面 

案例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Document</title>
    <style>

    </style>
</head>

<body>
    <div id="app">
        <base-layout>
            <p slot="header">标题信息</p>
            <p>主要内容1</p>
            <p>主要内容2</p>
            <p slot="footer">底部信息信息</p>
        </base-layout>
    </div>
</body>
<script src="./js/vue.js"></script>
<script>
    Vue.component('base-layout', {
        template: `
        <div>
            <header>
              <slot name="header"></slot>
            </header>
            <main>
              <slot></slot>
            </main>
            <footer>
                <slot name="footer"></slot>
            </footer>
        <slot>默认内容</slot>
        </div>
        `
    })

    let vm = new Vue({
        el: '#app',
        data: {}
    });
</script>

</html>

 运行:

第二种写法:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Document</title>
    <style>

    </style>
</head>

<body>
    <div id="app">
        <base-layout>
            <template slot="header">
            <p>标题信息</p>
        </template>
            <p>主要内容1</p>
            <p>主要内容2</p>
            <template slot="footer">
                <p>底部信息信息</p>
            </template>
        </base-layout>
    </div>
</body>
<script src="./js/vue.js"></script>
<script>
    Vue.component('base-layout', {
        template: `
        <div>
            <header>
              <slot name="header"></slot>
            </header>
            <main>
              <slot></slot>
            </main>
            <footer>
                <slot name="footer"></slot>
            </footer>
        <slot>默认内容</slot>
        </div>
        `
    })

    let vm = new Vue({
        el: '#app',
        data: {}
    });
</script>

</html>

4.作用域插槽

应用场景:父组件对子组件的内容进行加工处理 

①插槽定义

 

②插槽内容 

 

* 通过在子组件的插槽中通过v-bind绑定属性,在父组件中通过slot-scope属性获取到子组件的数据,进而对子组件内容进行加工处理 

案例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Document</title>
    <style>

    </style>
</head>

<body>
    <div id="app">
        <fruit-list :list="list">
            <template slot-scope="info">
                <strong v-if="info.info.id===2">{{info.info.name}}</strong>
                <span v-else>{{info.info.name}}</span>
            </template>
        </fruit-list>
    </div>
</body>
<script src="./js/vue.js"></script>
<script>
    Vue.component('fruit-list', {
        props: ['list'],
        template: `
        <div>
           <li :key="item.id" v-for="item in list">
           <slot :info="item">{{item.name}}</slot>
           </li>
        </div>
        `
    })

    let vm = new Vue({
        el: '#app',
        data: {
            list: [{
                id: 1,
                name: 'apple'
            }, {
                id: 2,
                name: 'banner'
            }]
        }
    });
</script>

</html>

 运行:

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cirrod

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值