Wang's blog Wang's blog
首页
  • 前端文章

    • HTML教程
    • CSS
    • JavaScript
  • 前端框架

    • Vue
    • React
    • VuePress
    • Electron
  • 后端技术

    • Npm
    • Node
    • TypeScript
  • 编程规范

    • 规范
  • 我的笔记
  • Git
  • GitHub
  • VSCode
  • Mac工具
  • 数据库
  • Google
  • 服务器
  • Python爬虫
  • 前端教程
更多
收藏
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Wang Mings

跟随大神,成为大神!
首页
  • 前端文章

    • HTML教程
    • CSS
    • JavaScript
  • 前端框架

    • Vue
    • React
    • VuePress
    • Electron
  • 后端技术

    • Npm
    • Node
    • TypeScript
  • 编程规范

    • 规范
  • 我的笔记
  • Git
  • GitHub
  • VSCode
  • Mac工具
  • 数据库
  • Google
  • 服务器
  • Python爬虫
  • 前端教程
更多
收藏
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Python爬虫

  • 前端教程

    • 团队规范

    • Project

      • Axios用法与原理
      • css布局技巧
      • 深度学习平台术语
      • 谈谈前端天花板问题
      • 一个程序员的成长之路
      • Markdown-It 解析原理
      • minipack源码解析
      • PostCSS
      • Electron工程踩坑记录
      • H5 Video踩坑记录
      • Puppeteer翻页爬虫
      • 重构你的javascript代码
      • RxJS入门实践
      • 官网脚手架思考与实践
      • Stylelint样式规范工具
      • TypeScript开发Vue应用
        • [#](#快速开发) 快速开发
        • [#](#_1-初始化项目) 1. 初始化项目
        • [#](#_2-引入typescript包) 2. 引入TypeScript包
        • [#](#_3-配置webpack) 3. 配置webpack
        • [#](#_4-eslint支持typescript) 4. ESLint支持TypeScript
          • [#](#_1-eslint插件-typescript) 1. ESLint插件 - TypeScript
          • [#](#_2-eslint配置) 2. ESLint配置
        • [#](#参考文章) 参考文章
      • Typescript tsconfig.json全解析
      • Vue项目TypeScript指南
      • TypeScript在Vue2.x中的坑
      • Vue Dialog弹窗解决方案
      • Vue JSX插件依赖及语法实践
      • Webpack 模块打包原理
      • Webpack4 配置详解
      • Webpack4 devServer配置详解
      • Webpack3.x升级Webpack4指南
    • JS

    • NodeJS

    • Vue

    • React

    • 效率工具

    • 读书笔记

  • 教程
  • 前端教程
  • Project
wangmings
2022-07-19
目录

TypeScript开发Vue应用

# # TypeScript开发Vue应用

本文旨在帮助读者如何在Vue应用中配置TypeScript,使得使用TypeScript强类型验证开发Vue应用。

# # 快速开发

如果想一键快速搭建TypeScript的Vue环境,github (opens new window) (opens new window)有人对vue-cli添加了typescript模板,可直接用来开发。vue-cli官方也正在开发类似的模板。如果对配置TypeScript有兴趣或者对现有应用改造,请接着看下文。

vue-cli3工具中直接集成了Vue + TypeScript选项,推荐使用。

    vue create xxx
    
1
2

以下为个人从基础的Webpack,到自定义搭建TypeScript应用的过程。

# # 1. 初始化项目

笔者使用vue-cli的master版本为基础,建立webpack项目。

    vue init webpack my-project
    
1
2

# # 2. 引入TypeScript包

既然需要用到typescript,那就要加入一些core包和第三方支持包。

    // 安装vue的官方插件
    npm i vue-class-component vue-property-decorator --save
    
    // ts-loader typescript 必须安装,其他的相信你以后也会装上的
    npm i ts-loader typescript tslint-loader --save-dev
    
1
2
3
4
5
6

# # 3. 配置webpack

  1. 找到./build/webpack.base.conf.js

  2. 将文件main.js改为main.ts,webpack.base.conf.js入口后缀也改下

  3. resove.extensions加入.ts

    resolve: {
        extensions: ['.js', '.vue', '.json', '.ts'],
        alias: {
          '@': resolve('src')
        }
      }
    
1
2
3
4
5
6
7
  1. 添加module.rules,使得webpack能解析.ts
    {
        test: /\.tsx?$/,
        loader: 'ts-loader',
        exclude: /node_modules/,
        options: {
        appendTsSuffixTo: [/\.vue$/],
        }
    }
    
1
2
3
4
5
6
7
8
9
  1. TypeScript 默认并不支持 *.vue 后缀的文件,创建src/typing/vue-shim.d.ts文件, typescript会自动加载解析.d.ts后缀文件。
    declare module "*.vue" {
      import Vue from "vue";
      export default Vue;
    }
    
1
2
3
4
5
  1. 改造.vue文件,尝试改为ts写法。将App.vue改为如下:
    <template>
     <div id="app">
       <img src="./assets/logo.png">
       <router-view/>
     </div>
    </template>
    
    <script lang="ts">
    import Vue from 'vue'
    import Component from 'vue-class-component'
    
    @Component({
     name: 'app'
    })
    export default class App extends Vue {}
    </script>
    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# # 4. ESLint支持TypeScript

TSLint不再被官方推荐,而使用[ESLint + Plugin]方式(https://github.com/typescript-eslint/typescript-eslint)替换。

# # 1. ESLint插件 - TypeScript

安装插件 + 配置(parse解析器),使得ESLint能约束TypeScript代码。官方github详细步骤 (opens new window) (opens new window)

  1. 必须包括的插件(必备):(这里的插件只约束Lint格式,TypeScript源码编译还得依赖babel插件支持)
    "typescript" // 必备
    "@typescript-eslint/parser" // 该解析器可以利用ESLint的默认解析器,并且可以代替ESLint的默认解析器espree
    "@typescript-eslint/eslint-plugin" // 特定于ESLint的插件,当与结合使用时@typescript-eslint/parser,可以运行特定于TypeScript的插入规则
    
1
2
3
4
  1. extends配置插件(可选):提前设置的规则集合
    "@vue/eslint-config-airbnb"
    "@vue/eslint-config-standard"
    "@vue/eslint-config-typescript"s
    
1
2
3
4

# # 2. ESLint配置

大部分于ESLint规则配置相似,最关键的:需要指定解析器:"parser": "@typescript-eslint/parser"

    "eslintConfig": {
        "root": true,
        "env": {
          "node": true
        },
        "extends": [
          // 可选配置文件
          "plugin:vue/essential",
          "@vue/standard",
          "@vue/typescript"
        ],
        "rules": {
          // 具体规则,可替换上述配置文件的规则,优先级最高
          "no-unused-vars": "off",
          "no-console": "off",
          "comma-dangle": "off",
          "semi": "off",
          "no-param-reassign": "off",
          // @typescript-eslint/eslint-plugin插件:支持特定于ts的规则
          "@typescript-eslint/no-var-requires": "off",
          "@typescript-eslint/no-explicit-any": "off",
          "@typescript-eslint/member-delimiter-style": "off",
          "@typescript-eslint/no-empty-function": "off",
          "@typescript-eslint/no-use-before-define": "off",
          "@typescript-eslint/camelcase": "off",
          "@typescript-eslint/semi": "off"
        },
        "parserOptions": {
            // 必备插件,识别ts语法
          "parser": "@typescript-eslint/parser"
        }
    }
    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

# # 参考文章

  • 如何用 ESLint 检查 TypeScript 代码 (opens new window) (opens new window)

  • vue-typescript-dpapp-demo (opens new window) (opens new window)

  • vue-typescript-starter (opens new window) (opens new window)

编辑 (opens new window)
Stylelint样式规范工具
Typescript tsconfig.json全解析

← Stylelint样式规范工具 Typescript tsconfig.json全解析→

最近更新
01
theme-vdoing-blog博客静态编译问题
09-16
02
搜索引擎
07-19
03
友情链接
07-19
更多文章>
Theme by Vdoing | Copyright © 2019-2022 Evan Xu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式