Vue.js+TypeScript

初始化工程

vue init webpack simple

工程结构如下:

  • simple/
    • build/
      • webpack.base.conf.js
    • src/
      • components/
        • Hello.vue
        • Count.vue
      • App.vue
      • main.js

TypeScript 安装与配置

npm i -g typescript

在工程根目录simple/下,执行tsc --init生成tsconfig.json,更改配置如下:

{
  "compilerOptions": {
    /* Basic Options */
    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */
    "module": "ESNext",                     /* Specify module code generation: 'none', commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    "lib": [
      "dom",
      "es5",
      "es2015.promise",
      "es2017",
      "esnext"
      ],                              /* Specify library files to be included in the compilation:  */
    "allowJs": true,                       /* Allow javascript files to be compiled. */

    /* Strict Type-Checking Options */
    "strict": true,                            /* Enable all strict type-checking options. */

    /* Module Resolution Options */
    "allowSyntheticDefaultImports": true,  /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */

    /* Experimental Options */
    "experimentalDecorators": true,        /* Enables experimental support for ES7 decorators. */
    "emitDecoratorMetadata": true         /* Enables experimental support for emitting type metadata for decorators. */
  }
}

Webpack 配置调整

npm i --save-dev typescript ts-loader

打开simple/build/webpack.base.conf.js,修改配置如下:

module.exports = {
  entry: {
    app: './src/main.ts'  // 将 main.js 重名为 main.ts 后,这里也要改
  },
...
  resolve: {
    extensions: ['.js', '.vue', '.json', '.ts'],  // 添加 .ts
    ...
  },
  module: {
    rules: [
      // 添加 ts-loader
      {
        test: /\.tsx?$/,
        loader: 'ts-loader',
        exclude: /node_modules/,
        options: {
          appendTsSuffixTo: [/\.vue$/],
        }
      },
      ...
    ]
  }
}

babel 配置

npm i --save-dev babel-plugin-transform-decorators-legacy babel-plugin-transform-class-properties

打开simple/.babelrc,添加配置:

"plugins": [
    "transform-decorators-legacy",
    "transform-class-properties",
    ...
  ]

单文件组件(Single File Components)

声明.vue文件为单文件组件,从中提取代码。
创建simple/src/sfc.d.ts,并写入如下内容:

declare module "*.vue" {
    import Vue from 'vue'
    export default Vue
}

组件配置

npm i --save-dev vue-class-component
npm i --save-dev vue-property-decorator
  • vue-property-decorator 完全依赖于 vue-class-component,可以只使用任意一种。

main.js

  • 重命名 main.jsmain.ts

App.vue

<script lang="ts">
import Vue from 'vue'
import Component from 'vue-class-component'

@Component
export default class App extends Vue {
  name: 'app'
}
</script>

Hello.vue

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <h2>{{ count }}</h2>
    <comm-count :msg="msg"></comm-count>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import Component from 'vue-class-component'

@Component({
  name: 'Hello',
  components: {
    CommCount: () => import('./Count.vue')
  }
})
export default class Hello extends Vue {
  // initial data
  msg: string = 'Welcome to Your Vue.js App'

  // computed
  get count () {
    return new Date().getSeconds()
  }

  // lifecycle hook
  mounted () {
    this.msg = 'Jack Ma'
    setInterval(this.greet, 1000)
  }

  // method
  greet () {
    this.msg = 'Jack Ma' + new Date().getSeconds()
  }
}
</script>

Count.vue

<template>
  <div>Count {{ msg }} {{ count }}</div>
</template>

<script lang="ts">
import Vue from 'vue'
import { Component, Prop } from 'vue-property-decorator'

@Component({
  name: 'Count'
})
export default class Count extends Vue {
  @Prop()
  msg: string
  
  count = 'sss, ' + this.msg
}
</script>


参考

Microsoft TypeScript Vue Starter
Vue.js TypeScript 支持
Writing Class-Based Components with Vue.js and TypeScript
vue-class-component
vue-property-decorator


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转载 :OpenDiggawesome-github-vue 是由OpenDigg整理并维护的Vue相关开源项目库...
    果汁密码阅读 23,176评论 8 124
  • 是不是你的微信里有这样一种人,你们不常联系,可看到他的头像就能想起一段往事。 是不是你的微信里有这样一种人,偶尔他...
    邵晓宁阅读 1,528评论 0 1
  • 一个人,吃到食堂没有人,一个人,待在图书馆待到没有人,空荡荡的地方,只有书包上挂着的小玩偶与我为伴。我给妈妈发...
    俏Wings阅读 274评论 0 0
  • 由于近期雨量较大,婺城区公路段工作人员在排查中发现金九线K17+700处半幅路面沉降严重,山上水流不经过...
    e11521c791ad阅读 165评论 0 0
  • 偶尔聊起 马尾白衣背包 偶然遇见,却已自然呆滞 余晖斜过你的面庞 小月牙的嘴角 ⋯⋯⋯⋯ 她说你很幸福 给了我解释...
    灯下阳光阅读 173评论 4 1