在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>

三、实际效果

相关推荐
mCell1 天前
GSAP ScrollTrigger 详解
前端·javascript·动效
gnip1 天前
Node.js 子进程:child_process
前端·javascript
excel1 天前
为什么在 Three.js 中平面能产生“起伏效果”?
前端
excel1 天前
Node.js 断言与测试框架示例对比
前端
天蓝色的鱼鱼1 天前
前端开发者的组件设计之痛:为什么我的组件总是难以维护?
前端·react.js
codingandsleeping1 天前
使用orval自动拉取swagger文档并生成ts接口
前端·javascript
石金龙1 天前
[译] Composition in CSS
前端·css
白水清风1 天前
微前端学习记录(qiankun、wujie、micro-app)
前端·javascript·前端工程化
Ticnix1 天前
函数封装实现Echarts多表渲染/叠加渲染
前端·echarts
用户22152044278001 天前
new、原型和原型链浅析
前端·javascript