[关闭]
@c-Ku 2018-03-08T10:16:12.000000Z 字数 2576 阅读 2084

Vue-cli 整合 Typescript 筆記(05/03/2018)

JavaScript VueJS


整理自:三命:Vue + TypeScript 新项目起手式
最新版:Vue-cli 整合 Typescript 筆記(05/03/2018)
示例应用:Vue-Demos(Vue + Vuex + Vue-Router + Typescript + less + Element)
按此步骤进行可保证流畅运行

初始化CLI脚手架

  1. // 首先安裝vue-cli
  2. $ sudo npm install vue-cli -g
  3. // 并初始化應用
  4. // 項目名稱、項目描述、項目作者、創建方式(默認)、是否引入路由(YES)、是否引入eslint代碼檢查(YES)、eslint風格(none)、引入單元測試工具(NO)、引入自動化測試工具(NO)
  5. $ vue init webpack newProject

安装(可能会)需要的插件

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

webpack 配置

找到 ./build/webpack.base.conf.js
修改 entry 入口 ts 为 js

  1. entry: {
  2. app: './src/main.ts'
  3. }

修改 resolve 以 import 时不加后缀

  1. resolve: {
  2. extensions: ['.js', '.vue', '.json', '.ts', '.tsx'],
  3. alias: {
  4. '@': resolve('src')
  5. }
  6. }

添加对应后缀文件 loader

  1. module: {
  2. rules: [
  3. // 从这里复制下面的代码就可以了
  4. {
  5. test: /\.ts$/,
  6. exclude: /node_modules/,
  7. enforce: 'pre',
  8. loader: 'tslint-loader'
  9. },
  10. {
  11. test: /\.tsx?$/,
  12. loader: 'ts-loader',
  13. exclude: /node_modules/,
  14. options: {
  15. appendTsSuffixTo: [/\.vue$/],
  16. }
  17. },
  18. // 复制以上的
  19. }
  20. }

ts-loader 会检索当前目录下的 tsconfig.json 文件,根据里面定义的规则来解析.ts文件(就跟.babelrc的作用一样)

tslint-loader 作用等同于 eslint-loader

添加 tsconfig 文件

根目录下创建 tslint.json
参考 tsconfig

添加 tslint

根目录下创建 tslint.json
意在引入 ts 的 standard 规范

  1. {
  2. "extends": "tslint-config-standard",
  3. "globals": {
  4. "require": true
  5. }
  6. }

让ts识别vue

创建 vue-shim.d.ts 放在项目文件目录下
比如 vue-clisrc 目录
意在使 typescript 识别 .vue 文件并交由 vue 模块处理

  1. declare module "*.vue" {
  2. import Vue from "vue";
  3. export default Vue;
  4. }

然后将文件目录下的 js文件 后缀改为 .ts
main.jsmain.ts
并在 ts 文件中 import .vue 文件时应加上完整后缀
typescript 默认不识别 .vue 文件

改造 .vue 文件

此处需要用到 vue-class-component 插件,该插件可使Vue语法更加直观、扁平。

可食用更高级的 vue-property-decorator 插件,除继承 vue-class-component 插件中已有 Component 外,新增了Emit, Inject, Model, Prop, Provide, Vue, Watch等六个装饰器

App.vue

  1. <template>
  2. <div id="app">
  3. <img src="./assets/logo.png">
  4. <router-view/>
  5. </div>
  6. </template>
  7. <script lang="ts">
  8. import Vue from 'vue'
  9. import Component from 'vue-class-component'
  10. @Component({})
  11. export default class App extends Vue {}
  12. </script>
  13. <style>
  14. #app {
  15. font-family: 'Avenir', Helvetica, Arial, sans-serif;
  16. -webkit-font-smoothing: antialiased;
  17. -moz-osx-font-smoothing: grayscale;
  18. text-align: center;
  19. color: #2c3e50;
  20. margin-top: 60px;
  21. }
  22. </style>

HellowWorld.vue

  1. <template>
  2. <div class="hello">
  3. <!-- omit -->
  4. </div>
  5. </template>
  6. <script lang="ts">
  7. import Vue from 'vue'
  8. import Component from 'vue-class-component'
  9. @Component({})
  10. export default class HelloWorld extends Vue {
  11. msg: string = 'Welcome to Your Vue.js App'
  12. }
  13. </script>
  14. <!-- Add "scoped" attribute to limit CSS to this component only -->
  15. <style scoped>
  16. h1,
  17. h2 {
  18. font-weight: normal;
  19. }
  20. ul {
  21. list-style-type: none;
  22. padding: 0;
  23. }
  24. li {
  25. display: inline-block;
  26. margin: 0 10px;
  27. }
  28. a {
  29. color: #42b983;
  30. }
  31. </style>

npm run dev

如报错
TypeError: Cannot read property 'afterCompile' of undefined.

需在 package.json 中更改以下包的版本号为
"ts-loader": "^3.5.0",
"webpack": "^3.10.0",

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注