在vue中使用typescript - 配置篇

以@vue/cli创建的项目

  1. 创建项目的时候, 选择typescript, 会自动添加好typescript
  2. 已有的项目添加typescript, 使用命令vue add typescript, 相关链接

自行配置webpack的项目

  1. npm下载依赖包
  • 安装 typescript, ts-loader, tslint, tslint-loader, tslint-config-standard, vue-property-decorator
  1. 增加 tsconfig.json
  • vue/cli 中的配置
  {
    "compilerOptions": {
      "target": "esnext",
      "module": "esnext",
      "strict": true,
      "jsx": "preserve",
      "importHelpers": true,
      "moduleResolution": "node",
      "experimentalDecorators": 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"
    ]
  }
  1. webpack.base.conf.js 中的配置
  resolve: {
      extensions: ['.js', '.vue', '.json', 'ts', 'tsx'], // 新增了'ts', 'tsx'
      alias: {
        'vue$': 'vue/dist/vue.esm.js',
        '@': resolve('src'),
      }
    },
    module: {
      rules: [
        {
          test: /\.ts$/,  // 用于加载项目中的ts文件
          exclude: /node_modules/,
          enforce: 'pre',
          loader: 'tslint-loader'
        },
        {
          test: /\.tsx?$/, // 用于加载项目中的tsx文件
          loader: 'ts-loader',
          exclude: /node_modules/,
          options: {
            appendTsSuffixTo: [/\.vue$/],
          }
        }]
  1. 重命名main.js
  • 将main.js重命名为main.ts
  • 在webpack.base.conf.js中修改入口的文件名 entry: {app: './src/main.ts'}
  1. 在main.ts同级目录下添加shims-tsx.d.ts 和 shims-vue.d.ts
  // vue/cli中的shims-tsx.d.ts
  // 作用: 为 JSX 语法的全局命名空间
  // 这是因为基于值的元素会简单的在它所在的作用域里按标识符查找
  // 此处使用的是无状态函数组件的方法来定义, 当在tsconfig内开启了jsx语法支持后, 其会自动识别对应的.tsx结尾的文件
  import Vue, { VNode } from 'vue'
  declare global {
    namespace JSX {
      // tslint:disable no-empty-interface
      interface Element extends VNode {}
      // tslint:disable no-empty-interface
      interface ElementClass extends Vue {}
      interface IntrinsicElements {
        [elem: string]: any
      }
    }
  }

  // vue/cli中的shims-vue.d.ts
  // 作用:为项目内所有的 vue 文件做模块声明, 毕竟 ts 默认只识别 .d.ts、.ts、.tsx 后缀的文件
  import Vue from "vue";
  import VueRouter, { Route } from "vue-router";

  declare module '*.vue' {
    export default Vue
  }

  declare module "vue/types/vue" {
    interface Vue {
      $router: VueRouter; // 这表示this下有这个东西
      $route: Route;
      $https: any;
      $urls: any;
      $Message: any;
      $Modal: any;
    }
  }
  1. 接下来就可以开发了, 开发的时候依赖 vue-property-decorator
  • 提供了 Vue Component, Prop, PropSync, Model, Watch, Provide, Inject, ProvideReactive, InjectReactive, Emit, Ref API
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Vue-Webpack-TypeScript 这是一个Vue2.5+版本兼容TypeScript开发的脚手架,虽然...
    Jacky_MYD阅读 2,801评论 0 14
  • 开发工具 vscode 众所周知,vue2+Typescript的开发体验很不好,不过为了尝鲜,咱还是可以搭一个小...
    北辰_狼月阅读 1,430评论 1 11
  • 整理自:三命:Vue + TypeScript 新项目起手式最新版:Vue-cli 整合 Typescript 筆...
    六毫笙阅读 2,053评论 1 2
  • 心理学家海因茨·科胡特说:“一个功能良好的心理结构,最重要的来源是父母的人格,特别是他们以不带敌意的坚决和不含诱惑...
    y诗淇阅读 71评论 0 0
  • 生活在海边的人,每天面对大海,不觉新鲜;生活在大山里的人,开门见山,平淡无奇。但是总会有人慕名而来,看山看海...
    正版云倾阅读 186评论 0 2