TypeScript+Vue 插件vue-property-decorator的使用总结

首先 下载javascript

npm install vue-class-component vue-property-decorator --save-dev

一梭子直接干;css

其次,咱来讲说它们的区别与联系:html

vue-property-decorator社区出品;vue-class-component官方出品vue

vue-class-component提供了Vue、Component等;java

vue-property-decorator深度依赖了vue-class-component,拓展出了更多操做符:@Prop、@Emit、@Inject、@Model、@Provide、@Watch;vuex

开发时正常引入vue-property-decorator就行npm

引入后写vue代码就是如此,ide

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

@Component
export default class App extends Vue {
  name:string = 'Simon Zhang'

  // computed
  get MyName():string {
    return `My name is ${this.name}`
  }

  // methods
  sayHello():void {
    alert(`Hello ${this.name}`)
  }

  mounted() {
    this.sayHello();
  }
}

至关于函数

export default {
  data () {
    return {
      name: 'Simon Zhang'
    }
  },

  mounted () {
    this.sayHello()
  },

  computed: {
    MyName() {
      return `My name is ${this.name}`
    }
  },

  methods: {
    sayHello() {
      alert(`Hello ${this.name}`)
    },
  }
}

大佬都说爽的一批;flex

然鹅菜鸟我遇到问题一堆,如下作个积累总结:

一、写法问题:引入组件和接收父组件传过来的参数

@Component({
  components: {
    XXXX
  },
  props: {
    mapFlag: Number
  }
})

二、获取refs,在其后面加上as HTMLDivElement(不知道是否是这插件引发的,懒得管,直接干就是)

let layoutList:any = this.$refs.layout as HTMLDivElement
或
let fieldCalculate:any = (this as any).$refs.fieldCalculate

三、ts文件 公用方法导出

const xxx = (value: any, type: string) => {
  ...
}
export { xxx, xxx, xxx, xxx }

四、引入装饰器,使用方式@Watch

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

五、引用插件。在shims-vue.d.ts 文件中声明,再在组件中引用

declare module 'vuedraggable' {
  const vuedraggable: any;
  export default vuedraggable;
}

六、@Watch使用

// 监听formula 其变化则显示验证公式按钮
    @Watch('formula', { deep: true, immediate: true }) formulaWatch (newVal:string, oldVal:string) {
      if (newVal !== oldVal) {
       this.grammarSuccess = true
       this.errMsgFlag = false
       this.checkFormulaSuccess = false
      }
    }

七、计算属性方法写法(跟@watch同样,当成方法写就行;加一个关键字get)

get aTagDatasFcomput () { // computed计算属性, 过滤出visible为true的对象来渲染,由于当 v-if 与 v-for 一块儿使用时,v-for 具备比 v-if 更高的优先级,这意味着 v-if 将分别重复运行于每一个 v-for 循环中
      return this.aTagDatasF.filter(item => item.visible)
}

八、@Prop的用法

@Prop({
      type: Boolean, // 父组件传递给子组件的数据类型
      required: false, // 是否必填
      default: false // 默认值, 若是传入的是 Object,则要 default: ()=>({}) 参数为函数
  }) collapsed !: boolean
@Prop()private datas!: any

感叹号是非null和非undefined的类型断言,因此上面的写法就是对datas这个属性进行非空断言

九、ts设置html的fontSize

获取时:(不加会报错Object is possibly 'null'.  真是一波骚操做)

let docEl = document.documentElement as HTMLDivElement // 加上 as HTMLDivElement

十、vuex-class的简单使用(简单的提示弹窗)

    首先在store.ts中命名一个state和mutations方法,例:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    tips: {
      show: false,
      title: ''
    }
  },
  changeTips: {
   selectOption (state, tips) {
      state.tips = tips
      setTimeout(() => {
        state.tips.show = false
      }, 1500)
    }
  }
})

而后在页面中引入 

import { State, Mutation } from 'vuex-class' // vuex-class

@Mutation changeTips: any // 引入该方法

this.changeTips({ show: true, title: '请选择沙雕' }) // 使用

该组件写法

<template>
  <!-- 提示弹层 -->
  <div class="tips" v-show="tips.show">
    <h3>{{tips.title}}</h3>
  </div>
</template>

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

@Component
export default class Tips extends Vue {
  // @State('tips') stateTips: any
  @State tips: any
}
</script>
<style  lang="scss" scoped>
.tips{
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 999;
  width: 100%;
  height: 100%;
  color: #fff;
  h3{
    padding: .08rem .2rem;
    max-width: 80%;
    font-size: .12rem;
    font-weight: 400;
    background-color: rgba(0,0,0,0.6);
    border-radius: 4px;
  }
}
</style>

 

 

此文长期慢慢累积

相关文章
相关标签/搜索