在vue项目里面使用index.ts进行统一导出

目录

一、概述

二、具体实践

2.1创建目录

2.2index.ts文件内容展示

2.2在需要的vue文件里面import

2.3vue全代码

三、实际效果


一、概述

一般我们在做项目的时候会发现vue文件里面没有export default

转而替代的是使用同目录下index.ts进行统一导出

好处:能够让项目的结构变的清晰,提高代码可维护性和可读性,统一管理导出

坏处:这样实质上效率没提升多少,反而不能立刻得知组件文件存放具体位置

二、具体实践

2.1创建目录

2.2index.ts文件内容展示

复制代码
import ClassAndStyleVue from "./ClassAndStyle.vue";
import computerAttributeVue from "./computerAttribute.vue";
import echartTestVue from "./echartTest.vue";
import HelloWorldVue from "./HelloWorld.vue";
import Hellolqd from "./HelloLqd.vue";
import htmlTest from "./htmlTest.vue";
import MyIfShowForVue from "./MyIfShowFor.vue";

export {
    ClassAndStyleVue ,
    computerAttributeVue,
    echartTestVue,
    HelloWorldVue,
    Hellolqd,
    htmlTest,
    MyIfShowForVue,

}

2.2在需要的vue文件里面import

复制代码
<script lang="ts" setup>

// import HelloWorld from '@/components/HelloWorld.vue'; // @ is an alias to /src
// import HelloKqd from '@/components/HelloLqd.vue';
// import HtmlTest from '@/components/htmlTest.vue'
// import ComputeAttribute from '@/components/computerAttribute.vue'
// import ClassAndStyle from '@/components/ClassAndStyle.vue'
// import MyIfShowFor from '@/components/MyIfShowFor.vue'

// import EchartTest from '@/components/echartTest.vue'
import {echartTestVue} from '@/components/index'

</script>

注意看,使用统一导出就需要使用解包了,而且制定到index目录

坏消息是我们不能像之前那样在import后面随便写名字了

好消息是我们依旧可以使用as 来进行重命名

2.3vue全代码

复制代码
<template>
  <div class="home">
    <!-- <img alt="Vue logo" src="../assets/logo.png"> -->
    <!-- <HelloWorld msg="Welcome to Your Vue.js + TypeScript App"/> -->
    <!-- <HelloKqd></HelloKqd> -->
    <!-- <HtmlTest></HtmlTest> -->
    <!-- <ComputeAttribute></ComputeAttribute> -->
    <!-- <ClassAndStyle></ClassAndStyle> -->
    <!-- <MyIfShowFor></MyIfShowFor> -->
    <!-- <EchartTest></EchartTest> -->
    <echartTestVue></echartTestVue>
  </div>
</template>

<script lang="ts" setup>

// import HelloWorld from '@/components/HelloWorld.vue'; // @ is an alias to /src
// import HelloKqd from '@/components/HelloLqd.vue';
// import HtmlTest from '@/components/htmlTest.vue'
// import ComputeAttribute from '@/components/computerAttribute.vue'
// import ClassAndStyle from '@/components/ClassAndStyle.vue'
// import MyIfShowFor from '@/components/MyIfShowFor.vue'

// import EchartTest from '@/components/echartTest.vue'
import {echartTestVue}  from '@/components/index'

</script>

三、实际效果

相关推荐
遇到困难睡大觉哈哈2 小时前
Harmony os 静态卡片(ArkTS + FormLink)详细介绍
前端·microsoft·harmonyos·鸿蒙
用户47949283569152 小时前
Bun 卖身 Anthropic!尤雨溪神吐槽:OpenAI 你需要工具链吗?
前端·openai·bun
p***43482 小时前
前端在移动端中的网络请求优化
前端
g***B7383 小时前
前端在移动端中的Ionic
前端
大猩猩X3 小时前
vxe-gantt 甘特图使用右键菜单
vue.js·vxe-table·vxe-ui·vxe-gantt
拿破轮3 小时前
使用通义灵码解决复杂正则表达式替换字符串的问题.
java·服务器·前端
whltaoin3 小时前
【 Web认证 】Cookie、Session 与 JWT Token:Web 认证机制的原理、实现与对比
前端·web·jwt·cookie·session·认证机制
Aerelin3 小时前
爬虫playwright入门讲解
前端·javascript·html·playwright
笙年4 小时前
JavaScript Promise,包括构造函数、对象方法和类方法
开发语言·javascript·ecmascript
桜吹雪4 小时前
LangChain.js/DeepAgents可观测性
javascript·人工智能