在 Vue 项目中注入外部 TypeScript 类

问题描述

我知道这是一个非常常见的问题,并且有很多答案,但我似乎找不到任何可行的解决方案或对如何使这个工作正常进行的很好的解释。

我在 Vue 中做了很多不同的项目,但我总是使用相同的文件夹结构和许多常见的 TypeScript 类、管理器和控制器。现在正因为如此,我真的很想把这些文件放在一个外部服务器上,并将它们包含在我所有的不同项目中。

这里要知道的最重要的事情是,我想这样做,这样每当我想更改其中一个常见类时就不必编辑每个项目。

我知道我可以只使用 NPM 来打包所有东西,但是每当 NPM 包发生变化时,我总是必须更新每个项目......

我正在使用的常见 TypeScript 类的一个示例是以下 FileManager:

import axios,{AxiosInstance,AxiosRequestConfig,AxiosResponse,ResponseType} from 'axios';

class FileManager {
  private _cdn: AxiosInstance = axios.create();

  public async fetchFileFromCdn (filePath: string,responseType?: ResponseType): Promise<FileResponse> {
    const response: FileResponse = {hasSucceeded: false};
    filePath = filePath + "?ignoreCache=" + new Date().getTime().toString();

    try {
      const config: AxiosRequestConfig = {};

      if (responseType) {
        config.responseType = responseType;
      }

      const axiosResponse: AxiosResponse = await this._cdn.get(filePath,config);
      response.result = axiosResponse.data;
      response.hasSucceeded = true;
    } catch (e) {
      const code: number = e.response && e.response.status ? e.response.status : 400;
      response.error = {code: code,isOperational: false,message: e.message};
      response.hasSucceeded = false;
    }
    return response;
  }
}

export default new FileManager();

export interface FileResponse {
  hasSucceeded: boolean;
  result?: any;
  error?: FileError;
}

export interface FileError {
  message: string;
  code: number;
  isOperational: boolean;
}

实现我想要的最佳方式是什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)