第十七课 component组件解析

component组件解析

component组件的写法在众多组件写法中算是比较简单的,component组件结构组成如下:

1)组件名

2)组件模板

3)利用Vue对象进行生成

基础示例:

复制代码
    <div id="app">
        <test></test>
    </div>

    <script>
        Vue.component('test', {
            template: '<h1>这是一个组件模板</h1>'
        })

        new Vue({
            el:'#app'
        })
    </script> 

component组件

我们可以通过在实例外部以component的方式拓展单个组件,当前方式需要在构建全局实例之前进行组件创建,不存在组件提升。(类似于变量提升)

1)创建组件

复制代码
    <div id="app">
        <test></test>
    </div>

    <script>
        Vue.component('test', {
            template: '<h1>这是一个组件模板</h1>'
        })

        new Vue({
            el:'#app'
        })
    </script> 

2)分离模板创建组件

复制代码
    <div id="app">
        <test></test>
    </div>

    <script>
        let tem = {
            template: '<h1>这是一个组件模板</h1>'
        }

        Vue.component('test', tem);

        new Vue({
            el:'#app'
        })
    </script> 

3)通过components拓展组件

components和methods类似,表示(组件)集合,我们也可以直接通过components进行组件拓展

复制代码
    <div id="app">
        <test></test>
    </div>

    <script>
        new Vue({
            el:'#app',
            components: {
                'test': {
                    template: '<h1>这是一个组件模板</h1>'
                }
            }
        })
    </script> 

驼峰式命名

驼峰式命名在使用的时候,需要将调用的组件名转换成横杠式

复制代码
    <div id="app">
        <test-me></test-me>
    </div>

    <script>
        Vue.component('testMe', {
            template: '<h1>这是一个组件模板</h1>'
        })

        new Vue({
            el:'#app'
        })
    </script>  

组件中的DOM包裹规则

1)单DOM组件

复制代码
    <div id="app">
        <test></test>
    </div>

    <script>
        Vue.component('test', {
            template: '<h1>这是一个组件模板</h1>'
        })

        new Vue({
            el:'#app'
        })
    </script> 

2)多DOM组件

如果组件涉及的DOM较多,必须要将所有DOM包裹在一个主DOM中,否则将报错

复制代码
    <div id="app">
        <test></test>
    </div>

    <script>
        Vue.component('test', {
            template: '<div><h1>这是一个组件模板</h1><h1>这是第二个组件模板</h1></div>'
        })

        new Vue({
            el:'#app'
        })
    </script> 

3)配合ES6模板语法使用

复制代码
    <div id="app">
        <test></test>
    </div>

    <script>
        Vue.component('test', {
            template: `<div>
                            <h1>这是一个组件模板</h1>
                            <h1>这是第二个组件模板</h1>
                      </div>`
        })

        new Vue({
            el:'#app'
        })
    </script> 
相关推荐
Uyker1 小时前
从零开始制作小程序简单概述
前端·微信小程序·小程序
Dontla4 小时前
为什么React列表项需要key?(React key)(稳定的唯一标识key有助于React虚拟DOM优化重绘大型列表)
javascript·react.js·ecmascript
EndingCoder5 小时前
React从基础入门到高级实战:React 实战项目 - 项目三:实时聊天应用
前端·react.js·架构·前端框架
阿阳微客6 小时前
Steam 搬砖项目深度拆解:从抵触到真香的转型之路
前端·笔记·学习·游戏
德育处主任Pro6 小时前
『React』Fragment的用法及简写形式
前端·javascript·react.js
CodeBlossom7 小时前
javaweb -html -CSS
前端·javascript·html
CodeCraft Studio7 小时前
【案例分享】如何借助JS UI组件库DHTMLX Suite构建高效物联网IIoT平台
javascript·物联网·ui
打小就很皮...7 小时前
HBuilder 发行Android(apk包)全流程指南
前端·javascript·微信小程序
集成显卡8 小时前
PlayWright | 初识微软出品的 WEB 应用自动化测试框架
前端·chrome·测试工具·microsoft·自动化·edge浏览器
前端小趴菜059 小时前
React - 组件通信
前端·react.js·前端框架