你真的完全了解vue组件的概念吗?

简介: 【10月更文挑战第7天】你真的完全了解vue组件的概念吗?

非单文件组件

非单文件组件就是一个文件里面有n个组件 n>1

Vue中使用组件的三大步骤:

一、定义组件(创建组件)

使用Vue.extend(options)创建,其中options和new Vue(options)时传入的那个options几乎一样,但也有点区别;

区别如下:

  • el不要写,为什么? ——— 最终所有的组件都要经过一个vm的管理,由vm中的el决定服务哪个容器。
  • data必须写成函数,为什么? ———— 避免组件被复用时,数据存在引用关系。

备注:使用template可以配置组件结构。

//第一步:创建school组件
const school = Vue.extend({
   
    template:`
            <div class="demo">
                    <h2>学校名称:{
    {schoolName}}</h2>
                    <h2>学校地址:{
    {address}}</h2>
                    <button @click="showName">点我提示学校名</button>    
            </div>
    `,
// el:'#root', //组件定义时,一定不要写el配置项,因为最终所有的组件都要被一个vm管理,由vm决定服务于哪个容器。
    data(){
   
    return {
   
        schoolName:'11111',
        address:'陕西西安'
    }
    },
    methods: {
   
        showName(){
   
            alert(this.schoolName)
        }
    },
})

二、注册组件

局部注册

靠new Vue的时候传入components选项

new Vue({
   
    el:'#root',
    data:{
   
            msg:'你好啊!'
    },
    //第二步:注册组件(局部注册)
    components:{
   
            school,
    }
})

全局注册

靠Vue.component('组件名',组件)

//第一步:创建hello组件
const hello = Vue.extend({
   
        template:`
            <div>    
                    <h2>你好啊!{
    {name}}</h2>
            </div>
        `,
        data(){
   
            return {
   
               name:'Tom'
            }
        }
})

//第二步:全局注册组件
  Vue.component('hello',hello)

三、使用组件(写组件标签)

vue.js为源码文件,以下文件直接进行了本地引入

链接:https://pan.baidu.com/s/1D8p8Ywxfo5PCIw_aF8xeNw

提取码:lhgv

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>基本使用</title>
        <script type="text/javascript" src="../js/vue.js"></script>
    </head>
    <body>
        <!-- 准备好一个容器-->
        <div id="root">
      <!-- 第三步:编写组件标签 -->
            <hello></hello>
            <hr>
            <h1>{
  {msg}}</h1>
            <hr>
            <!-- 第三步:编写组件标签 -->
            <school></school>
            <hr>
            <!-- 第三步:编写组件标签 -->
            <student></student>
        </div>

        <div id="root2">
            <hello></hello>
        </div>
    </body>

    <script type="text/javascript">
        Vue.config.productionTip = false

        //第一步:创建school组件
        const school = Vue.extend({
    
            template:`
                <div class="demo">
                    <h2>学校名称:{
     {schoolName}}</h2>
                    <h2>学校地址:{
     {address}}</h2>
                    <button @click="showName">点我提示学校名</button>    
                </div>
            `,
            data(){
    
                return {
    
                    schoolName:'尚硅谷',
                    address:'北京昌平'
                }
            },
            methods: {
    
                showName(){
    
                    alert(this.schoolName)
                }
            },
        })

        //第一步:创建student组件
        const student = Vue.extend({
    
            template:`
                <div>
                    <h2>学生姓名:{
     {studentName}}</h2>
                    <h2>学生年龄:{
     {age}}</h2>
                </div>
            `,
            data(){
    
                return {
    
                    studentName:'张三',
                    age:18
                }
            }
        })

        //第一步:创建hello组件
        const hello = Vue.extend({
    
            template:`
                <div>    
                    <h2>你好啊!{
     {name}}</h2>
                </div>
            `,
            data(){
    
                return {
    
                    name:'Tom'
                }
            }
        })

        //第二步:全局注册组件
        Vue.component('hello',hello)

        //创建vm
        new Vue({
    
            el:'#root',
            data:{
    
                msg:'你好啊!'
            },
            //第二步:注册组件(局部注册)
            components:{
    
                school,
                student
            }
        })

        new Vue({
    
            el:'#root2',
        })
    </script>
</html>

组件使用注意点

关于组件名:

一个单词组成:

第一种写法(首字母小写):school

第二种写法(首字母大写):School

多个单词组成:

第一种写法(kebab-case命名):my-school

第二种写法(CamelCase命名):MySchool (需要Vue脚手架支持)

备注:

(1).组件名尽可能回避HTML中已有的元素名称,例如:h2、H2都不行。

(2).可以使用name配置项指定组件在开发者工具中呈现的名字。

关于组件标签:

第一种写法:

第二种写法: (需要使用脚手架,不用使用脚手架时,会导致后续组件不能渲染。)

一个简写方式

const school = Vue.extend(options) 可简写为:const school = options

组件的嵌套

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>组件的嵌套</title>
        <!-- 引入Vue -->
        <script type="text/javascript" src="../js/vue.js"></script>
    </head>
    <body>
        <!-- 准备好一个容器-->
        <div id="root"></div>
    </body>

    <script type="text/javascript">
        Vue.config.productionTip = false //阻止 vue 在启动时生成生产提示。
        //定义student组件
        const student = Vue.extend({
    
            name:'student',
            template:`
                <div>
                    <h2>学生姓名:{
     {name}}</h2>    
                    <h2>学生年龄:{
     {age}}</h2>    
                </div>
            `,
            data(){
    
                return {
    
                    name:'尚硅谷',
                    age:18
                }
            }
        })
        //定义school组件
        const school = Vue.extend({
    
            name:'school',
            template:`
                <div>
                    <h2>学校名称:{
     {name}}</h2>    
                    <h2>学校地址:{
     {address}}</h2>    
                    <student></student>
                </div>
            `,
            data(){
    
                return {
    
                    name:'尚硅谷',
                    address:'北京'
                }
            },
            //注册组件(局部)
            components:{
    
                student
            }
        })

        //定义hello组件
        const hello = Vue.extend({
    
            template:`<h1>{
     {msg}}</h1>`,
            data(){
    
                return {
    
                    msg:'欢迎来到尚硅谷学习!'
                }
            }
        })

        //定义app组件
        const app = Vue.extend({
    
            template:`
                <div>    
                    <hello></hello>
                    <school></school>
                </div>
            `,
            components:{
    
                school,
                hello
            }
        })

        //创建vm
        new Vue({
    
            template:'<app></app>',
            el:'#root',
            //注册组件(局部)
            components:{
    app}
        })
    </script>
</html>

VueComponent

  • 组件本质是一个名为VueComponent的构造函数,且不是程序员定义的,是Vue.extend生成的。
//定义school组件
    const school = Vue.extend({
   
            name:'school',
            template:`
                    <div>
                      哈哈哈哈
                    </div>
            `,
    })
console.log(school)   
//    ƒ VueComponent (options) {
   
//         this._init(options);
//    }

从打印结果可以看出,shool组件实际是由VueComponent构造函数所实例化出来的对象。

打断点,进入vue.js源码中,可以发现在使用 Vue.extend创建组件时,实际还在内部调用了VueComponent构造函数生成了sub实例化对象,最终将实例化对象return了出来。

  • 我们只需要写或,Vue解析时会帮我们创建school组件的实例对象,即Vue帮我们执行的:new VueComponent(options)。
  • 特别注意:每次调用Vue.extend,返回的都是一个全新的VueComponent!!!!

  • 关于this指向:
    1. 组件配置中:data函数、methods中的函数、watch中的函数、computed中的函数 它们的this均是【VueComponent实例对象】。
    2. new Vue(options)配置中:data函数、methods中的函数、watch中的函数、computed中的函数 它们的this均是【Vue实例对象】。
<!DOCTYPE html>
<html>
    <head>
            <meta charset="UTF-8" />
            <title>VueComponent</title>
            <script type="text/javascript" src="../js/vue.js"></script>
    </head>
    <body>
            <div id="root">
                    <school></school>
                    <hello></hello>
            </div>
    </body>

    <script type="text/javascript">
        Vue.config.productionTip = false

        //定义school组件
        const school = Vue.extend({
    
            name:'school',
            template:`
                    <h1 @click="showThis">
                            点击school组件
                    </h1>
            `,
            methods: {
    
                    showThis(){
    
                            console.log('school组件',this)
                    }
            },
        })

        const test = Vue.extend({
    
             template:`<h4>test子组件</h4>`
        })

        //定义hello组件
        const hello = Vue.extend({
    
            template:`
                    <h2>
                            <h4>hello组件</h4>
                            <test></test>    
                    </h2>
            `,
            components:{
    test}
        })

        //创建vm
        const vm = new Vue({
    
            el:'#root',
            created(){
    
                    console.log("Vue根组件",this)
            },
            components:{
    school,hello}
        })
    </script>
</html>

此外。也可以看出,vue根组件管理者其他所有组件

  • VueComponent的实例对象,以后简称vc(也可称之为:组件实例对象)。Vue的实例对象,以后简称vm。

一个重要的内置关系

如图,正常情况下,VueComponent的隐式原型属性proto应该指向Object的原型对象,但是vue做了强制更改,使

VueComponent.prototype.proto === Vue.prototype

为什么要有这个关系:让组件实例对象(vc)可以访问到 Vue原型上的属性、方法。

vue2中的组件


原生html中组件的使用

不使用Cli脚手架的项目中


全局使用组件(全局注册)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>我的练习</title>
    </head>
    <body>
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

        <div id="app">
            <my-Button></my-Button>
                 <my-Button></my-Button>
        </div>

        <script type="text/javascript">
        // 定义一个名为 button-counter 的新组件
        Vue.component('MyButton', {
    
          data: function () {
    
            return {
    
              count: 0
            }
          },
          template: '<button v-on:click="count++">我被点击了{
    { count }}次</button>'
        })
         const vm = new Vue({
    
            el: '#app',
            data: {
    }
        })
        </script>
    </body>
</html>

组件名的两种命名方式

连字符:如 "my-Button",使用

驼峰式:如"MyButton",或均可。

  • 直接在 DOM (即非字符串的模板) 中使用时只有连字符是有效的,如上述例子使用会报错
  • 组件中的data必须是函数

局部使用组件(局部注册)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>我的练习</title>
    </head>
    <body>
        <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

        <div id="app">
            <b>{
  {hello}}</b>
            <my-Button1></my-Button1>
        </div>
        <script type="text/javascript">

        const Bcomponent = {
    
          data: function() {
    return {
    title: "嘿嘿!"}},
          template: '<button>{
    { title }}</button>'
        };

        const Pcomponent = {
    
          data: function() {
    return {
    count: 9}},
          components:{
    
              MyButton2:Bcomponent,
          },
          template: '<div><button>{
    { count }}次数</button><my-Button2></my-Button2></div>'
        };

        const vm = new Vue({
    
            el: '#app',
            data: {
    hello:"你好"},
            components:{
    
                MyButton1:Pcomponent,
            }
        })
        </script>
    </body>
</html>

cli中组件的使用

通过cli创建的项目

全局使用组件(全局注册)

import Thecomponent from './Thecomponent.vue'
Vue.component('MyButton',Thecomponent )
const vm = new Vue({
   
    render:h => h(App)
}).$mount("#app")

局部使用组件(局部注册)

某一vue文件中

<template>
  <div class="app">
    <Child></Child>
  </div>
</template>
<script>
    import Child from './components/Child.vue'//静态引入
    export default {
   
        name:'App',
        components:{
    Child:Child },
    }
</script>

Vue3中组件

原生html中组件的使用

不使用Cli脚手架的项目中

<!DOCTYPE html>
<html>
    <head>
            <meta charset="utf-8">
            <title>我的练习</title>
    </head>
    <body>
        <script src="https://unpkg.com/vue@next"></script>

        <div id="app">
                <button-counter></button-counter>
        </div>
        <script type="text/javascript">

        // 创建一个Vue 应用
        const app = Vue.createApp({
    
                data() {
    
                    return {
    
                      counter: 0
                    }
                  }
        })

        // 定义一个名为 button-counter 的新全局组件
        app.component('button-counter', {
    
          data() {
    
            return {
    
              count: 0
            }
          },
          template: `
            <button @click="count++">
              我被点击了 {
     { count }} 次
            </button>`
        })
        app.mount('#app')

        </script>
    </body>
</html>

vite中

全局使用组件(全局注册)

const app = Vue.createApp({
   ...})

app.component('my-component-name', {
   
  /* ... */
})

app.mount('#app')

局部使用组件(局部注册)

<template>
    <div class="app">
        <Child ></Child>
    </div>
</template>
<script>
    import Child from './components/Child.vue'//静态引入
    export default {
   
        name:'App',
        components:{
   Child},  //组件写法不变
    }
</script>

方法同vue2,不变

相关文章
|
2天前
|
编解码 Java 程序员
写代码还有专业的编程显示器?
写代码已经十个年头了, 一直都是习惯直接用一台Mac电脑写代码 偶尔接一个显示器, 但是可能因为公司配的显示器不怎么样, 还要接转接头 搞得桌面杂乱无章,分辨率也低,感觉屏幕还是Mac自带的看着舒服
|
4天前
|
存储 缓存 关系型数据库
MySQL事务日志-Redo Log工作原理分析
事务的隔离性和原子性分别通过锁和事务日志实现,而持久性则依赖于事务日志中的`Redo Log`。在MySQL中,`Redo Log`确保已提交事务的数据能持久保存,即使系统崩溃也能通过重做日志恢复数据。其工作原理是记录数据在内存中的更改,待事务提交时写入磁盘。此外,`Redo Log`采用简单的物理日志格式和高效的顺序IO,确保快速提交。通过不同的落盘策略,可在性能和安全性之间做出权衡。
1540 5
|
1月前
|
弹性计算 人工智能 架构师
阿里云携手Altair共拓云上工业仿真新机遇
2024年9月12日,「2024 Altair 技术大会杭州站」成功召开,阿里云弹性计算产品运营与生态负责人何川,与Altair中国技术总监赵阳在会上联合发布了最新的“云上CAE一体机”。
阿里云携手Altair共拓云上工业仿真新机遇
|
7天前
|
人工智能 Rust Java
10月更文挑战赛火热启动,坚持热爱坚持创作!
开发者社区10月更文挑战,寻找热爱技术内容创作的你,欢迎来创作!
578 22
|
4天前
|
存储 SQL 关系型数据库
彻底搞懂InnoDB的MVCC多版本并发控制
本文详细介绍了InnoDB存储引擎中的两种并发控制方法:MVCC(多版本并发控制)和LBCC(基于锁的并发控制)。MVCC通过记录版本信息和使用快照读取机制,实现了高并发下的读写操作,而LBCC则通过加锁机制控制并发访问。文章深入探讨了MVCC的工作原理,包括插入、删除、修改流程及查询过程中的快照读取机制。通过多个案例演示了不同隔离级别下MVCC的具体表现,并解释了事务ID的分配和管理方式。最后,对比了四种隔离级别的性能特点,帮助读者理解如何根据具体需求选择合适的隔离级别以优化数据库性能。
201 3
|
10天前
|
JSON 自然语言处理 数据管理
阿里云百炼产品月刊【2024年9月】
阿里云百炼产品月刊【2024年9月】,涵盖本月产品和功能发布、活动,应用实践等内容,帮助您快速了解阿里云百炼产品的最新动态。
阿里云百炼产品月刊【2024年9月】
|
10天前
|
Linux 虚拟化 开发者
一键将CentOs的yum源更换为国内阿里yum源
一键将CentOs的yum源更换为国内阿里yum源
578 5
|
23天前
|
存储 关系型数据库 分布式数据库
GraphRAG:基于PolarDB+通义千问+LangChain的知识图谱+大模型最佳实践
本文介绍了如何使用PolarDB、通义千问和LangChain搭建GraphRAG系统,结合知识图谱和向量检索提升问答质量。通过实例展示了单独使用向量检索和图检索的局限性,并通过图+向量联合搜索增强了问答准确性。PolarDB支持AGE图引擎和pgvector插件,实现图数据和向量数据的统一存储与检索,提升了RAG系统的性能和效果。
|
6天前
|
XML 安全 Java
【Maven】依赖管理,Maven仓库,Maven核心功能
【Maven】依赖管理,Maven仓库,Maven核心功能
233 3
|
9天前
|
存储 人工智能 搜索推荐
数据治理,是时候打破刻板印象了
瓴羊智能数据建设与治理产品Datapin全面升级,可演进扩展的数据架构体系为企业数据治理预留发展空间,推出敏捷版用以解决企业数据量不大但需构建数据的场景问题,基于大模型打造的DataAgent更是为企业用好数据资产提供了便利。
327 2