当前位置:网站首页>07 【动态组件 组件注册】
07 【动态组件 组件注册】
2022-08-10 09:03:00 【DSelegent】
13.动态组件
有些场景会需要在两个组件间来回切换,比如 Tab 界面:
<script setup>
import Home from './Home.vue'
import Posts from './Posts.vue'
import Archive from './Archive.vue'
import { ref } from 'vue'
const currentTab = ref('Home')
const tabs = {
Home,
Posts,
Archive
}
</script>
<template>
<div class="demo">
<button
v-for="(tab, index) in tabs"
:key="index"
:class="['tab-button', { active: currentTab === index }]"
@click="currentTab = index"
>
{
{ tab }}
</button>
<component :is="tabs[currentTab]" class="tab"></component>
</div>
</template>
<style>
.tab-button.active {
background: #e0e0e0;
}
</style>

上面的例子是通过 Vue 的 <component> 元素和特殊的 is attribute 实现的:
<!-- currentTab 改变时组件也改变 -->
<component :is="tabs[currentTab]"></component>
在上面的例子中,被传给 :is 的值可以是以下几种:
- 被注册的组件名
- 导入的组件对象
你也可以使用 is attribute 来创建一般的 HTML 元素。
当使用 <component :is="..."> 来在多个组件间作切换时,被切换掉的组件会被卸载。我们可以通过 `` 组件强制被切换掉的组件仍然保持“存活”的状态。
14.组件注册
14.1 全局注册#
我们可以使用 Vue 应用实例的 app.component() 方法,让组件在当前 Vue 应用中全局可用。
import {
createApp } from 'vue'
const app = createApp({
})
app.component(
// 注册的名字
'MyComponent',
// 组件的实现
{
/* ... */
}
)
如果使用单文件组件,你可以注册被导入的 .vue 文件:
import MyComponent from './test.vue'
app.component('MyComponent', MyComponent)
app.component() 方法可以被链式调用:
app
.component('ComponentA', ComponentA)
.component('ComponentB', ComponentB)
.component('ComponentC', ComponentC)
全局注册的组件可以在此应用的任意组件的模板中使用:
<!-- 这在当前应用的任意组件中都可用 -->
<ComponentA/>
<ComponentB/>
<ComponentC/>
所有的子组件也可以使用全局注册的组件,这意味着这三个组件也都可以在彼此内部使用。
14.2 局部注册#
全局注册虽然很方便,但有以下几个问题:
- 全局注册,但并没有被使用的组件无法在生产打包时被自动移除 (也叫“tree-shaking”)。如果你全局注册了一个组件,即使它并没有被实际使用,它仍然会出现在打包后的 JS 文件中。
- 全局注册在大型项目中使项目的依赖关系变得不那么明确。在父组件中使用子组件时,不太容易定位子组件的实现。和使用过多的全局变量一样,这可能会影响应用长期的可维护性。
相比之下,局部注册的组件需要在使用它的父组件中显式导入,并且只能在该父组件中使用。它的优点是使组件之间的依赖关系更加明确,并且对 tree-shaking 更加友好。
在使用 <script setup> 的单文件组件中,导入的组件可以直接在模板中使用,无需注册:
<script setup>
import ComponentA from './ComponentA.vue'
</script>
<template>
<ComponentA />
</template>
如果没有使用 <script setup>,则需要使用 components 选项来显式注册:
import ComponentA from './ComponentA.js'
export default {
components: {
ComponentA
},
setup() {
// ...
}
}
对于每个 components 对象里的属性,它们的 key 名就是注册的组件名,而值就是相应组件的实现。上面的例子中使用的是 ES2015 的缩写语法,等价于:
export default {
components: {
ComponentA: ComponentA
}
// ...
}
请注意:局部注册的组件在后代组件中并*不*可用。在这个例子中,ComponentA 注册后仅在当前组件可用,而在任何的子组件或更深层的子组件中都不可用。
14.3 组件名格式#
在整个指引中,我们都使用 PascalCase 作为组件名的注册格式,这是因为:
- PascalCase 是合法的 JavaScript 标识符。这使得在 JavaScript 中导入和注册组件都很容易,同时 IDE 也能提供较好的自动补全。
<PascalCase />在模板中更明显地表明了这是一个 Vue 组件,而不是原生 HTML 元素。同时也能够将 Vue 组件和自定义元素 (web components) 区分开来。
在单文件组件和内联字符串模板中,我们都推荐这样做。但是,PascalCase 的标签名在 DOM 模板中是不可用的,详情参见 DOM 模板解析注意事项。
为了方便,Vue 支持将模板中使用 kebab-case 的标签解析为使用 PascalCase 注册的组件。这意味着一个以 MyComponent 为名注册的组件,在模板中可以通过 <MyComponent> 或 <my-component> 引用。这让我们能够使用同样的 JavaScript 组件注册代码来配合不同来源的模板。
边栏推荐
- ShardingSphere入门
- ARM Architecture 3: Addressing and Exception Handling of ARM Instructions
- Nvidia's gaming graphics card revenue plummets / Google data center explosion injures 3 people / iPhone battery percentage returns... More news today is here...
- mysql千万级别数据库优化
- js reads excel time format conversion
- 【REST架构】OData、JsonAPI、GraphQL 有什么区别?
- Flink部署 完整使用 (第三章)
- 【 WeChat applet 】 read page navigation
- UE4 Sequence添加基础动画效果 (05-蓝图触发Sequence)
- 关于判断单峰数组的几种方法
猜你喜欢

怎么使用【jmeter正则表达式提取器】解决返回值作参数的问题

Spotify使用C4模型表达其架构设计

郭晶晶家的象棋私教,好家伙是个机器人
![How to use [jmeter regular expression extractor] to solve the problem of returning the value as a parameter](/img/bf/2367304d5bdf520e369097a49a7bb6.png)
How to use [jmeter regular expression extractor] to solve the problem of returning the value as a parameter

【API架构】REST API 行业辩论:OData vs GraphQL vs ORDS

shell iterates over folders and outputs

JWT: To own me is to have power

Pieces of TensorFlow 2.9 (1)

DeepFake换脸诈骗怎么破?让他侧个身

I don't want to do accounting anymore, Die changed to a new one, moved forward bravely, and finally successfully passed the career change test to double his monthly salary~
随机推荐
js读取excel时间格式转换
Spotify expresses its architectural design using the C4 model
大佬们,请问一下,oraclecdc报错没有序列化,可是我看源码中的确是没有继承序列化的,是什么原因
英伟达游戏显卡营收暴跌/ 谷歌数据中心爆炸致3人受伤/ iPhone电量百分比回归…今日更多新鲜事在此...
NaiveUI中看起来没啥用的组件(文字渐变)实现原来这么简单
关于判断单峰数组的几种方法
J9数字论:关于DAO 特点的宏观分析
【REST架构】OData、JsonAPI、GraphQL 有什么区别?
【元宇宙欧米说】听兔迷兔如何从虚拟到现实创造潮玩新时代
[OAuth2] Nineteen, OpenID Connect dynamic client registration
凭借这份阿里架构师的万字面试手册,逆风翻盘,斩获阿里offer
OLTP and OLAP database architecture 】 【 : actual comparison
【系统设计】S3 对象存储
线程池的基本概念、结构、类
Nvidia's gaming graphics card revenue plummets / Google data center explosion injures 3 people / iPhone battery percentage returns... More news today is here...
ARM体系结构2:处理器内核和汇编指令集
基于sklearn的决策树应用实战
高等数学(第七版)同济大学 习题4-3 个人解答
1499. The maximum pile.then/deque
Different command line styles