vue系列之 vue-cli3 引入 typescript


1.vue-class-component
強化 Vue 組件,使用 TypeScript/裝飾器 增強 Vue 組件

2.vue-property-decorator
在 vue-class-component 上增強更多的結合 Vue 特性的裝飾器

3.vuex-class
基於vue-class-component對Vuex提供的裝飾器

vue-class-component

 

<script lang="ts">
  import Vue from 'vue'
  import Component from 'vue-class-component'
  @Component
  export default class App extends Vue {
    // 初始化數據
    msg = 123
    // 聲明周期鈎子
    mounted () {
      this.greet()
    }
    // 計算屬性
    get computedMsg () {
      return 'computed ' + this.msg
    }
    // 方法
    greet () {
      alert('greeting: ' + this.msg)
    }
  }
</script>
 
改造.vue
  1. <script>標簽添加lang="ts"聲明
  2. 代碼中導入 *.vue 文件的時候,需要寫上 .vue 后綴  
 
 
原因還是因為 TypeScript 默認只識別 .ts 文件,不識別 .vue 文件
import Component from 'components/component.vue'

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM