@vue/cli typescript插件使用指南
步骤
- 使用 yarn add 安装 @vue/cli-service 对应版本的 @vue/cli-plugin-typescript
- 例如:"@vue/cli-service": "~4.5.0" 使用
yarn add -D @vue/cli-plugin-typescript@^4安装
- 例如:"@vue/cli-service": "~4.5.0" 使用
- 使用 vue invoke typescript 运行插件
- 插件提供的配置项
- Use class-style component syntax? 是否使用类组件
- 类组件是通过 typescript 提供的装饰器实现了通过写一个类来写 vue 组件的方法,对 typescript 有更好的支持。
- 但是官方配套的库并不能完美解决 typescript 的支持,需要 vue-tsx-support 提供额外支持
- 新项目建议不选择,直接使用 composition API,虽然它也需要 vue-tsx-support 提供支持,但是这种代码组织方式更解耦
- Use Babel alongside TypeScript (required for modern mode, auto-detected polyfills, transpiling JSX)? 是否在 typescript 编译后使用 babel 进行编译
- typescript 具备转换 ts 到指定某 es 版本的能力,但是不具备 babel 提供的其他转换代码的能力。
- 例如:typescript 虽然能够把 jsx 转换为 javascript,但是转换的结果不能满足 vue 的要求,依然需要 babel 进行二次转换
- 建议开启:jsx 是 vue 使用 ts 比较成熟的方案
- Convert all .js files to .ts? 是否转换所有的 js 为 ts 文件
- 建议选择否。对于旧项目而言,这种操作会导致大量的 ts 文件类型报错
- Allow .js files to be compiled?
- 是否允许接收 js 文件作为入口文件
- Skip type checking of all declaration files (recommended for apps)?
- 跳过所有类型文件 .d.ts 的检查,因为类型文件通常是外部库提供的,检查这些类型文件将会降低编译速度
- Use class-style component syntax? 是否使用类组件
- 运行插件后续钩子
- 如果项目中已经安装了 eslint 插件,由于增加了对 typescript 的格式检查支持,eslint 的钩子会被调用
- 建议在格式化之前暂存文件,然后恢复被格式化的文件
- 如果项目中已经安装了 eslint 插件,由于增加了对 typescript 的格式检查支持,eslint 的钩子会被调用
相关问题
最佳实践
- vue组件需要类型支持的特性
- props
- emit
- slot
- scopeSlot
- ref 获取组件的方法或属性
- provide/inject
- vue 对 ts 的支持并不完善,建议放弃 .vue 文件方式,改用 jsx 书写 template
- .vue 文件在引用组件时需要通过插件 Volar 来识别组件的类型,插件容易面临支持不完善和编辑器卡顿的问题
- 使用 jsx 书写,需要配合 class component 或 compositionAPI 来实现,个人建议采用 compositionAPI,它在代码组织上更为灵活
- vue 提供的 vue.extend 和 defineComponent 方法并不能正确返回一个 class 类型供外部使用,需要 wonderful-panda/vue-tsx-support提供完善的类型支持
- 提供了原生元素的类型支持(input等)
- 支持 props、emit、scopeSlot、ref 类型
- 支持对现有组件进行类型包装,即给现有组件增加类型,使其能够被 ts 正确识别
- 支持 class component 和 compositionAPI
- 提供 router-link and router-view 类型支持
<MyComponent props={{ foo: "foo" }} />;- 事件处理程序包装器,其工作方式类似于模板中可用的一些事件修饰符,例如:
<div onKeydown={m.enter(this.onEnter)} />;
- tsx 先由 ts 转换为 js,然后由 babel 转换为 vue 渲染函数,ts 对部分 jsx 语法不支持(见后文)
别名
- 使用 tsconfig-paths-webpack-plugin,把 tsconfig.json 中配置的别名同步到 webpack 中
// vue.config.js
{
chainWebpack: (config) => {
config.resolve
.plugin("tsconfig-paths")
.use(require("tsconfig-paths-webpack-plugin"));
},
}
jsx
- 3.0 和 4.0 的 jsx 是由 base 的插件提供的,由 @vue/babel-preset-jsx 实现
<p>hello { this.message }</p><input type="email" placeholder={this.placeholderText} /><input { ...{ attrs: {type: 'email'}} } /><header slot="header">header</header><MyComponent scopedSlots={ {header: () => <header>header</header>} } /><p domPropsInnerHTML={html} />
- @vue/babel-preset-jsx
- @vue/babel-helper-vue-jsx-merge-props
- @vue/babel-plugin-transform-vue-jsx
- @vue/babel-sugar-composition-api-inject-h
setup() { return () => <button /> }
- @vue/babel-sugar-composition-api-render-instance
- 支持
setup() { return () => <button /> }中绑定 setup 中定义的数据return () => <MyComponent vModel={a.b} />
- 支持
- @vue/babel-sugar-functional-vue
export default ({ props }) => <p>hello {props.message}</p>orconst HelloWorld = ({ props }) => <p>hello {props.message}</p>
- @vue/babel-sugar-inject-h
- @vue/babel-sugar-v-model
<input vModel_trim={this.newTodoText} />ts中有效
- @vue/babel-sugar-v-on
<input vOn:click={this.newTodoText} />- ts中无效,可以写为
<input onClick={this.newTodoText} />
- ts中无效,可以写为
<input vOn:click_stop_prevent={this.newTodoText} />- ts中无效?
compositionAPI
相关文件
3.0
- package.json
"devDependencies": {
"@vue/cli-plugin-babel": "^3.12.0",
"@vue/cli-service": "^3.12.0",
"vue-template-compiler": "^2.6.10",
"sass": "^1.19.0",
"sass-loader": "^8.0.0",
"@vue/cli-plugin-eslint": "^3.12.0",
"@vue/eslint-config-prettier": "^5.0.0",
"eslint": "^5.16.0",
"eslint-plugin-prettier": "^3.1.0",
"eslint-plugin-vue": "^5.0.0",
"lint-staged": "^8.1.5",
"prettier": "^1.18.2",
"babel-eslint": "^10.0.1",
"@vue/eslint-config-typescript": "^4.0.0",
"@vue/cli-plugin-typescript": "^3.12.0",
"typescript": "^3.4.3",
},
- tsconfig.json
{
"compilerOptions": {
"target": "esnext", // 指定ECMAScript目标版本
"module": "esnext", // 指定生成哪个模块系统代码
"strict": true, // 启用所有严格检查选项
"jsx": "preserve", // 在.tsx文件里支持JSX
"importHelpers": true, // 从tslib导入辅助工具函数
"moduleResolution": "node", // 决定如何处理模块,解析引用时查找模块的规则
"esModuleInterop": true, // 修复 CommonJS/AMD/UMD模块 导入语句转换问题
"allowSyntheticDefaultImports": true, // 允许从没有设置默认导出的模块中默认导入。这并不影响代码的输出,仅为了类型检查。
"sourceMap": true, // 生成相应的.map文件
"baseUrl": ".", // 解析非相对模块名的基准目录
"types": [ // 要包含的类型声明文件名列表
"webpack-env"
],
"paths": { // 别名,基于baseUrl的路径映射的列表
"@/*": [
"src/*"
]
},
"lib": [ // 编译过程中需要引入的库文件的列表
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
// 使用"include"引入的文件可以使用"exclude"属性过滤。 然而,通过"files"属性明确指定的文件却总是会被包含在内,不管"exclude"如何设置
// 任何被"files"或"include"指定的文件所引用的文件也会被包含进来
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],
"exclude": [
"node_modules"
]
}
- .eslintrc.js
module.exports = {
root: true,
env: {
node: true
},
extends: ["plugin:vue/essential", "@vue/prettier", "@vue/typescript"], // 和仅仅 eslint 的区别
rules: {
"no-console": process.env.NODE_ENV === "production" ? "error" : "off",
"no-debugger": process.env.NODE_ENV === "production" ? "error" : "off"
},
parserOptions: {
parser: "@typescript-eslint/parser" // 和仅仅 eslint 的区别
}
};
4.0
- package.json
"devDependencies": {
"@vue/cli-plugin-babel": "~4.5.19",
"@vue/cli-service": "~4.5.19",
"vue-template-compiler": "^2.6.11",
"sass": "^1.26.5",
"sass-loader": "^8.0.2",
"@vue/cli-plugin-router": "~4.5.19",
"@vue/cli-plugin-vuex": "~4.5.19",
"@vue/cli-plugin-eslint": "~4.5.19",
"@vue/eslint-config-prettier": "^6.0.0",
"eslint": "^6.7.2",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-vue": "^6.2.2",
"lint-staged": "^9.5.0",
"prettier": "^2.2.1",
"@vue/eslint-config-typescript": "^7.0.0",
"@vue/cli-plugin-typescript": "~4.5.19",
"typescript": "~4.1.5",
"@typescript-eslint/eslint-plugin": "^4.18.0",
"@typescript-eslint/parser": "^4.18.0"
},
- tsconfig.json
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"baseUrl": ".",
"types": [
"webpack-env"
],
"paths": {
"@/*": [
"src/*"
]
},
"lib": [
"esnext",
"dom",
"dom.iterable",
"scripthost"
]
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
"tests/**/*.ts",
"tests/**/*.tsx"
],
"exclude": [
"node_modules"
]
}
- .eslintrc.js
module.exports = {
root: true,
env: {
node: true,
},
extends: [
"plugin:vue/essential",
"eslint:recommended",
"@vue/typescript/recommended", // 和仅仅 eslint 的区别,注意顺序
"@vue/prettier",
"@vue/prettier/@typescript-eslint", // 和仅仅 eslint 的区别,注意顺序
],
parserOptions: {
ecmaVersion: 2020, // // 和仅仅 eslint 的区别
},
rules: {
"no-console": process.env.NODE_ENV === "production" ? "warn" : "off",
"no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off",
},
};
@vue/cli typescript插件使用指南的更多相关文章
- Vue CLI 是如何实现的 -- 终端命令行工具篇
Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统,提供了终端命令行工具.零配置脚手架.插件体系.图形化管理界面等.本文暂且只分析项目初始化部分,也就是终端命令行工具的实现. 0. 用法 ...
- vue cli创建typescript项目
使用最新的Vue CLI @vue/cli创建typescript项目,使用vue -V查看当前的vue cli版本 安装命令 npm install -g @vue-cli 创建项目 vue cre ...
- vue cli 3
介绍 Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统 通过 @vue/cli 搭建交互式的项目脚手架. 通过 @vue/cli + @vue/cli-service-global 快 ...
- Vue CLI 介绍安装
https://cli.vuejs.org/zh/guide/ 介绍 警告 这份文档是对应 @vue/cli 3.x 版本的.老版本的 vue-cli 文档请移步这里. Vue CLI 是一个基于 V ...
- [WIP]Vue CLI
更新: 2019/05/30 文档: https://cli.vuejs.org/zh/ 安装 npm install -g @vue/cli 确认是否成功安装 vue --version 基础 ...
- vue系列之vue cli 3引入ts
插件 Vue2.5+ Typescript 引入全面指南 vue-class-component强化 Vue 组件,使用 TypeScript/装饰器 增强 Vue 组件 vue-property-d ...
- Vue CLI 3.x 简单体验
文档 中文文档 补充于02月10日 vue脚手架的3.x版本已经在开发中,现在还处于alpha版本.我们来看看有哪些变化. 使用 npm install -g @vue/cli 命名方式已经改为npm ...
- Vue技术点整理-Vue CLI安装详解
一,脚手架安装 Node 版本要求 Vue CLI 需要 Node.js +).你可以使用 nvm 或 nvm-windows 在同一台电脑中管理多个 Node 版本. 1,全局安装Vue CLI ...
- [Vue 牛刀小试]:第十七章 - 优化 Vue CLI 3 构建的前端项目模板(1)- 基础项目模板介绍
一.前言 在上一章中,我们开始通过 Vue CLI 去搭建属于自己的前端 Vue 项目模板,就像我们 .NET 程序员在使用 asp.net core 时一样,我们更多的会在框架基础上按照自己的开发习 ...
- vue cli 3.0快速创建项目
本地安装vue-cli 前置条件 更新npm到最新版本 命令行运行: npm install -g npmnpm就自动为我们更新到最新版本 淘宝npm镜像使用方法 npm config set reg ...
随机推荐
- yarn -D和-S区别
-D和-S区别 安装的环境不同 -D是--save-dev的简写,会安装在开发环境中(production)中的devPendencies中 -S是--save的简写,会安装在生产环境中(develo ...
- oeasy教您玩转vim - 72 - # 缩写abbreviation
缩写abbreviation 回忆上次折叠的细节 这次了解到了:mkview.:loadview 保存和加载视图 可以把当前的状态保存下来 可以在 viewoption 中配置保存选项,设置哪些需 ...
- Jmeter强制结束线程
例子:正常的线程是执行2次请求 1.需要实现结果 执行请求1后,判断test1=100,强制结束线程 执行请求1后,判断test1 != 100,继续执行请求2 2. 线程组改造 在请求1后面增加[i ...
- 对比python学julia(第二章)--(第一节)隔沟算样—枚举策略
1.1. 问题描述 在明代数学家程大位的<算法统宗>著作中记载了这样一道数学题: 甲乙隔沟放牧,二人暗里参详. 甲云得乙九个羊,多你一倍之上. 乙说得甲九只,两家之数相当. 两边闲坐恼心 ...
- 【Excel】VBA编程 02 访问MySQL数据库
1.配置Windows连接驱动ODBC 因为是访问MySQL,则对应的ODBC驱动由MySQL厂商发布 https://dev.mysql.com/downloads/connector/odbc/ ...
- ViT:拉开Trasnformer在图像领域正式挑战CNN的序幕 | ICLR 2021 —— An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale
论文地址: https://arxiv.org/abs/2010.11929 Github地址: https://github.com/google-research/vision_transform ...
- nvtop —— GPU使用情况的可视化工具
安装: sudo apt install nvtop
- 阿里提供的免费pypi镜像服务器
介绍页地址: https://developer.aliyun.com/mirror/pypi 具体的镜像地址: https://mirrors.aliyun.com/pypi/
- gpg 密钥的导入、导出
参考: gpg 密钥生成.导入.导出.自动输入密码 How do I delete secret subkeys correctly? 注意: 本文不对GPG的操作做详细介绍,有深入了解者自行参考:g ...
- python版本的“共轭梯度法”算法代码
在看代码的过程中遇到了共轭梯度法这个概念,对这个算法的数学解释看过几遍,推导看过了,感觉懂了,然后过上一些日子就又忘记了,然后又看了一遍推导,然后过了一些日子也就又忘记了,最后想想这个算法的数学解释就 ...