vue3項(xiàng)目typescript如何export引入(imported)的interface問題
vue3 typescript如何export引入的interface
引入接口后,不能原封不動地直接export出去。
typescript支持面向?qū)ο笳Z言中常見的接口(interface)、類(class)等。
但我近幾天發(fā)現(xiàn),一個(gè)interface,通過import引入后,如果直接再export出去,是不行的。
語法沒有錯(cuò),但運(yùn)行時(shí)似乎出問題。
比如,我寫一個(gè)組件timeline,文件結(jié)構(gòu)如下圖所示。
為規(guī)范其他模塊調(diào)用,我在_type.ts中定義了一個(gè)接口,是關(guān)于數(shù)據(jù)類型的。
按照組件編寫的套路,是在組件根目錄下有一個(gè)index.ts,里面集成各種類型、部件,方便外部引用:
1、index.ts
import TimeLine from './_timeLine.vue' import { EColor, IActivityItem as IActivityItemBase } from './_type' export { TimeLine, EColor } export type IActivityItem = IActivityItemBase
2、_type.ts
export enum EColor { blue = '#427df9', green = '#34a27f', red = '#c73641', gray = '#e4e7ed' } export interface IActivityItem { title: string time: string done: boolean color?: string }
3、如何將接口IActivityItem傳導(dǎo)出去?
在index.ts集成IActivityItem,就是想將它傳導(dǎo)出去的。
但是,如前所述,IActivityItem并沒有直接定義在index.ts,而是在_type.ts中定義。
之所以這樣做,是因?yàn)榻M件的核心部件_timeline.ts也要用到它,由于index.ts有導(dǎo)入_timeline.ts,如果接口定義在index.ts,那么_timeline.ts勢必要引用index.ts,這樣就出現(xiàn)一個(gè)循環(huán)。
我也不知道會不會有問題,但想想都不靠譜。
測試過程中,發(fā)現(xiàn)在index.ts中引入IActivityItem,然后又export出去,系統(tǒng)會卡住,不知道哪里出了問題:
import { EColor, IActivityItem } from './_type' export { EColor,IActivityItem }
枚舉EColor這樣做沒有問題,接口IActivityItem就有問題,不知道什么原因。
最后的解決辦法是繼承一下,再export出去:
import { EColor, IActivityItem as IActivityItemBase } from './_type' export { EColor } export type IActivityItem = IActivityItemBase
vue3使用interface定義props
//此處注引入 import {PropType} from 'vue' interface Cells{ col:string, row:string, field:any } interface Subform { cells:Array<Cells> } //定義 const Props = { subform:{ type:Object as PropType<Sumbform>, default:{} }, }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue導(dǎo)出excel的兩種實(shí)現(xiàn)方式代碼
這篇文章主要給大家介紹了關(guān)于vue導(dǎo)出excel的兩種實(shí)現(xiàn)方式,在項(xiàng)目中我們可能會碰到導(dǎo)出Excel文件的需求,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2023-08-08

Vue3實(shí)現(xiàn)點(diǎn)擊按鈕實(shí)現(xiàn)文字變色功能

vue-print-nb解決vue打印問題,并且隱藏頁眉頁腳方式

vue2 v-model/v-text 中使用過濾器的方法示例