欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue-cli3項(xiàng)目引入Typescript的實(shí)現(xiàn)方法

 更新時(shí)間:2019年10月18日 09:59:13   作者:TJ-XiaJiaHao  
這篇文章主要介紹了Vue-cli3項(xiàng)目引入Typescript的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

假設(shè)已經(jīng)有一個(gè)通過 vue-cli3 腳手架構(gòu)建的 vue 項(xiàng)目

命令行安裝 Typescript

npm install --save-dev typescript
npm install --save-dev @vue/cli-plugin-typescript

編寫 Typescript 配置

根目錄下新建 tsconfig.json,下面為一份配置實(shí)例(點(diǎn)擊查看所有配置項(xiàng))。值得注意的是,默認(rèn)情況下,ts 只負(fù)責(zé)靜態(tài)檢查,即使遇到了錯(cuò)誤,也僅僅在編譯時(shí)報(bào)錯(cuò),并不會(huì)中斷編譯,最終還是會(huì)生成一份 js 文件。如果想要在報(bào)錯(cuò)時(shí)終止 js 文件的生成,可以在 tsconfig.json 中配置 noEmitOnError 為 true。

{
 "compilerOptions": {
  "target": "esnext",
  "module": "esnext",
  "strict": true,
  "importHelpers": true,
  "moduleResolution": "node",
  "experimentalDecorators": true,
  "esModuleInterop": true,
  "allowSyntheticDefaultImports": true,
  "sourceMap": true,
  "baseUrl": ".",
  "allowJs": false,
  "noEmit": true,
  "types": [
   "webpack-env"
  ],
  "paths": {
   "@/*": [
    "src/*"
   ]
  },
  "lib": [
   "esnext",
   "dom",
   "dom.iterable",
   "scripthost"
  ]
 },
 "exclude": [
  "node_modules"
 ]
}

新增 shims-vue.d.ts

根目錄下新建 shims-vue.d.ts,讓 ts 識(shí)別 *.vue 文件,文件內(nèi)容如下

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

修改入口文件后綴

src/main.js => src/main.ts

改造 .vue 文件

.vue 中使用 ts 實(shí)例

// 加上 lang=ts 讓webpack識(shí)別此段代碼為 typescript
<script lang="ts">
 import Vue from 'vue'
 export default Vue.extend({
  // ...
 })
</script>

一些好用的插件

vue-class-component:強(qiáng)化 Vue 組件,使用 TypeScript裝飾器 增強(qiáng) Vue 組件,使得組件更加扁平化。點(diǎn)擊查看更多

import Vue from 'vue'
import Component from 'vue-class-component'

// 表明此組件接受propMessage參數(shù)
@Component({
  props: {
    propMessage: String
  }
})
export default class App extends Vue {
  // 等價(jià)于 data() { return { msg: 'hello' } }
  msg = 'hello';
  
  // 等價(jià)于是 computed: { computedMsg() {} }
  get computedMsg() {
    return 'computed ' + this.msg
  }
  
  // 等價(jià)于 methods: { great() {} }
  great() {
    console.log(this.computedMsg())
  }
}

vue-property-decorator:在 vue-class-component 上增強(qiáng)更多的結(jié)合 Vue 特性的裝飾。點(diǎn)擊查看更多

import { Vue, Component, Prop, Watch, Emit } from 'vue-property-decorator'

@Component
export default class App extends Vue {
  @Prop(Number) readonly propA: Number | undefined
  @Prop({ type: String, default: ''}) readonly propB: String
  
  // 等價(jià)于 watch: { propA(val, oldval) { ... } }
  @Watch('propA')
  onPropAChanged(val: String, oldVal: String) {
    // ...
  }
  
  // 等價(jià)于 resetCount() { ... this.$emit('reset') }
  @Emit('reset')
  resetCount() {
    this.count = 0
  }
  
  // 等價(jià)于 returnValue() { this.$emit('return-value', 10, e) }
  @Emit()
  returnValue(e) {
    return 10
  }
  
  // 等價(jià)于 promise() { ... promise.then(value => this.$emit('promise', value)) }
  @Emit()
  promise() {
    return new Promise(resolve => {
      resolve(20)
    })
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論