vue3 + ts项目(无vite)报错记录

记录项目创建后遇到的报错
1.类型"Window & typeof globalThis"上不存在属性"_CONFIG"。ts(2339)

问题描述:

使用全局 window 上自定义的属性,TypeScript 会报属性不存在

解决:需要将自定义变量扩展到全局 window 上,在项目的xxx.d.ts文件添加如下代码声明

javascript 复制代码
declare interface Window {
  _CONFIG: any
}

添加后好像得重启编译器

2.axios报错
2.1 问题描述:

这个报错是因为axios版本更新后出现了新的类型导致的问题,可以根据此贴中的描述来解决,也可以用回旧版本解决,我是用回了0.21.x的旧版本

javascript 复制代码
yarn add axios@^0.21.1
2.2 类型"AxiosResponse"上不存在属性"success"。ts(2339)

解决:封装文件中追加声明描述

javascript 复制代码
import axios, { AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios'

declare module "axios" {
  interface AxiosResponse<T = any> {
    result:any
    success:any,
    // 这里追加你的参数
  }
  export function create(config?: AxiosRequestConfig): AxiosInstance;
}
3.vue.config.js路径别名配置
javascript 复制代码
module.exports = defineConfig({
  transpileDependencies: true,
  lintOnSave: false,
  publicPath: "/",
  outputDir: "dist",
  chainWebpack: config => {
    config.resolve.alias
      .set('@', resolve('src'))
      .set('@assets', resolve('src/assets'))
      .set('@views', resolve('src/views'))
      .set('@comp', resolve('src/components'))
  }
});
4.项目引入第三方库的关键字报错

问题描述:

这里就是第三方库的关键字DC报错了

解决:还是在xxx.d.ts文件中追加关键字声明

javascript 复制代码
declare var DC: any
相关推荐
深海鱼在掘金12 小时前
深入浅出RAG——第7章:基础篇实战:文档问答机器人
人工智能·typescript·命令行
苏灿烤鱼16 小时前
Cursor 官方插件仓,许可证未声明
typescript·agent·cursor
濮水大叔16 小时前
CabloyJS 强大之处不仅仅是 IoC,而是全栈资源的寻址体系
typescript·node.js·全栈
To_OC1 天前
踩了个 TS 的坑之后,我终于把 type 和 interface 掰明白了
前端·react.js·typescript
mCell1 天前
用 Cordis 从零构建一个 Mini DeepSeek Harness
typescript·agent·deepseek
满栀5851 天前
状态管理:Redux、Vuex、Pinia 核心区别
前端·javascript·typescript
苏灿烤鱼2 天前
公司**不可计算,就自己做操作系统
rust·typescript·agent
sunly_2 天前
TypeScript总结:16、面向对象速查
前端·javascript·typescript
gs801402 天前
解构 Cordis:面向“时空可组合性”的 TypeScript 元框架深度剖析
前端·javascript·typescript
sunly_2 天前
TypeScript总结:15、类型速查
前端·javascript·typescript