vue中使用ts配置步骤

通过前端各个框架的发展,例如vue3.0,react和angular等框架的源码都是用ts(TypeScripe)进行编写的,因此我感觉未来的中大型项目的发展趋势也离不开ts。因此我根据一些入门教程利用vue结合ts编写了文档,适合入门配置vue+ts项目。

1、vue老项目引入TypeScripe

npm install vue-class-component vue-property-decorator --save
npm install ts-loader typescript tslint tslint-loader tslint-config-standard --save-dev

vue-class-component扩展vue支持typescript,将原有的vue语法通过声明的方式来支持ts

vue-property-decorator基于vue-class-component扩展更多装饰器

ts-loader让webpack能够识别ts文件

tslint-loadertslint用来约束文件编码,可装可不装,建议最好安装下,有利于代码规范

tslint-config-standardtslint 配置 standard风格的约束,这个也是用来规范ts代码风格的

注:这种方式安装ts是为了将原有的vue项目中Js语法修改为Ts,详细步骤参考https://www.cnblogs.com/webhmy/p/13690836.html此博客中对于vue.config.js或者低版本的webpack.base.conf中配置支持ts语法展示不太完全,因此我修改如下:

// 对于文件插件配置,需要写在configureWebpack这个对象中。
module.exports = {
    configureWebpack: {
        resolve: { extensions: [".ts", ".tsx", ".js", ".json"] },
        module: {
            rules: [
                {
                    test: /\.ts$/,
                    exclude: /node_modules/,
                    enforce: 'pre',
                    loader: 'tslint-loader'
                },
                {
                    test: /\.tsx?$/,
                    loader: 'ts-loader',
                    exclude: /node_modules/,
                    options: {
                        appendTsSuffixTo: [/\.vue$/],
                    }
                }
            ]
        }
    }
}

从零开始创建vue+TypeScripe项目

这种方式比较简单,只要在用命令vue create app-name创建项目时选择自定义就可以创建,如下步骤:
在这里插入图片描述
在这里插入图片描述
第二步选中上面几种就行,在终端中利用空格键进行选中,选中之后回车,选择项含义如下:

 (*) Babel   //ES6转ES5
 (*) TypeScript   //使用ts
 ( ) Progressive Web App (PWA) Support   //渐进式Web应用
 (*) Router  //路由
 (*) Vuex  //状态管理
 (*) CSS Pre-processors  //CSS预处理
 (*) Linter / Formatter   //规范类型
 ( ) Unit Testing  //测试
 ( ) E2E Testing  //测试

下一步的配置细节如下:

Use class-style component syntax? (Y/n)   是否使用class风格的组件语法   输入Y回车

Use Babel alongside TypeScript (required for modern mode, auto-detected polyfills, trans
piling JSX)? (Y/n)   是否使用Babel和TypeScript(现代模式、自动检测多边形填充、trans所需(JSX) 输入Y回车

Use history mode for router? (Requires proper server setup for index fallback in product
ion) (Y/n)    是否使用history路由模式  输入N回车

Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default)  选择预处理器模式 我常选择Sass/SCSS (with node-sass)

Pick a linter / formatter config: (Use arrow keys):选择语法检测规范  一般默认第一个ESLint with error prevention only, 但是使用ts可以选择TSLint

Pick additional lint features: (Press to select, to toggle all, to invert selection)   选择保存时检查 / 提交时检查  一般开发时选择第一个保存时检查

Where do you prefer placing config for Babel, PostCSS, ESLint, etc.? (Use arrow keys)   选择配置信息存放位置,单独存放或者并入package.json  一般也是默认选择第一个,插件配置单独存放在一个文件

Save this as a preset for future projects? (y/N)   是否保存为预设,这样下次创建项目就不用重新选择  输入N回车

以上选项完成后项目就搭建成功了,项目目录如下:
在这里插入图片描述
vue.config.js这个文件需要自己创建,放在项目根目录下即可

要在Vue 3使用TypeScript配置响应拦截器,你需要进行以下步骤: 1. 首先,确保已经安装了axios和@types/axios库。可以使用以下命令进行安装: ``` npm install axios @types/axios ``` 2. 在你的Vue项目创建一个拦截器文件,比如 `interceptors.ts`。在这个文件,你可以定义请求和响应的拦截器逻辑。 ```typescript import axios, { AxiosInstance } from 'axios'; // 创建一个Axios实例 const instance: AxiosInstance = axios.create({ baseURL: 'https://api.example.com', // 设置你的API基本URL timeout: 5000, // 设置请求超时时间 }); // 请求拦截器 instance.interceptors.request.use( (config) => { // 在发送请求之前做一些处理,比如添加请求头 config.headers.common['Authorization'] = 'Bearer token'; return config; }, (error) => { // 处理请求错误 return Promise.reject(error); } ); // 响应拦截器 instance.interceptors.response.use( (response) => { // 在接收响应数据之前做一些处理,比如解析响应数据 return response.data; }, (error) => { // 处理响应错误 return Promise.reject(error); } ); export default instance; ``` 3. 在你的Vue组件使用拦截器。你可以在需要发送HTTP请求的地方导入 `interceptors.ts` 文件并使用创建的Axios实例进行请求。 ```typescript import axiosInstance from './interceptors'; // 使用拦截器发送请求 axiosInstance.get('/api/data') .then((response) => { console.log(response); }) .catch((error) => { console.error(error); }); ``` 这样,你就可以在Vue 3使用TypeScript配置响应拦截器了。记得根据你的实际需求修改拦截器逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值