Vue3使用Typescript常见问题
# 在 ts 中 axios 返回 not exist on type 'AxiosResponse'
vue 的 axios 请求返回值调取时报下面这错误
Property 'content' does not exist on type 'AxiosResponse<any>'.Vetur(2339)
1
解决办法,修改 ts 声明文件.d.ts
加入以下内容
declare module 'axios' {
import * as axios from 'axios'
interface AxiosInstance {
(config: AxiosRequestConfig): Promise<any>
}
}
1
2
3
4
5
6
2
3
4
5
6
# 路由懒加载
需要使用defineAsyncComponent
来完成懒加载
import { defineAsyncComponent } from 'vue'
const _import = (path:string) => defineAsyncComponent(() => import(`/src/view/${path}.vue`));
const routes = [
{
path: '/',
name: 'login',
component: _import('account/login'),
mate: {noAuth: true}
}
]
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 找不到模块“../views/HomeView.vue”或其相应的类型声明。
//vite-env.d.ts
declare module "*.vue" {
import { App, defineComponent } from "vue";
const component: ReturnType<typeof defineComponent> & {
install(app: App): void;
};
export default component;
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
上次更新: 2023/09/22, 16:54:32