关于如何在vue中使用typescript(第一天)

导读:本篇文章讲解 关于如何在vue中使用typescript(第一天),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

关于如何在vue中使用typescript(第一天)

环境

  • Vue2
  • Typescript

创建项目

vue create demo

在这里插入图片描述

在这里插入图片描述

目录介绍

在这里插入图片描述

基本介绍

使用

开始前我们先来了解一下在 vue 中使用 typescript 非常好用的几个库

vue-class-component

vue-class-component是一个 Class Decorator,也就是类的装饰器

vue-property-decorator

vue-property-decorator是基于 vue 组织里 vue-class-component 所做的拓展import { Vue, Component, Inject, Provide, Prop, Model, Watch, Emit, Mixins } from ‘vue-property-decorator’

vuex-module-decorators

用 typescript 写 vuex 很好用的一个库import { Module, VuexModule, Mutation, Action, MutationAction, getModule } from ‘vuex-module-decorators’

组件声明

ts版

在这里插入图片描述

js版

在这里插入图片描述

data 对象

ts版

在这里插入图片描述

  • !: 表示一定存在,?: 表示可能不存在。这两种在语法上叫赋值断言
  • public表示外部ts可调用
  • private只能在当前ts使用
  • protected表示继承类可使用

js版

在这里插入图片描述

props 对象

ts版

在这里插入图片描述

js版

在这里插入图片描述

method

ts版

在这里插入图片描述

js版

在这里插入图片描述

watch

ts版

在这里插入图片描述
@Watch(path: string, options: WatchOptions = {})
options 包含两个属性 immediate?:boolean 侦听开始之后是否立即调用该回调函数 / deep?:boolean 被侦听的对象的属性被改变时,是否调用该回调函数

@Watch(‘arr’, { immediate: true, deep: true }) onArrChanged(newValue: number[], oldValue: number[]) {}

js版

在这里插入图片描述

computed 计算属性

ts版

<template>
  <input v-model="name">
</template>
 
<script>
import Vue from 'vue'
import Component from 'vue-class-component'
 
@Component
export default class HelloWorld extends Vue {
  firstName = 'John'
  lastName = 'Doe'
 
  // Declared as computed property getter
  get name() {
    return this.firstName + ' ' + this.lastName
  }
 
  // Declared as computed property setter
  set name(value) {
    const splitted = value.split(' ')
    this.firstName = splitted[0]
    this.lastName = splitted[1] || ''
  }
}
</script>

js版

<template>
  <el-table border :data="data" style="width: 100%;" height="400" @selection-change="selectChange">
    <el-table-column type="selection" width="55"></el-table-column>
    <el-table-column prop="code" label="编码"></el-table-column>
    <el-table-column prop="name" label="名称"></el-table-column>
  </el-table>
</template>
 
<script>
export default {
  name: 'hierarchy-table',
  props: {
    value: {
      type: Array,
      required: true
    },
    skipCodes: {
      type: Array
    }
  },
  data() {
    return {
    };
  },
  computed: {
    data() {
      return this.skipCodes ? this.value.filter(it => !this.skipCodes.includes(it.code)) : this.value;
    }
  },
  methods: {
    selectChange(selection) {
      this.$emit('selection-change', selection);
    }
  }
};
</script>

emit 事件

Ts

import { Vue, Component, Emit } from 'vue-property-decorator'
@Component
export default class MyComponent extends Vue {
  count = 0
  @Emit()
  addToCount(n: number) {
      this.count += n
  }
  @Emit('reset')
  resetCount() {
      this.count = 0
  }
  @Emit()
  returnValue() {
      return 10
  }
  @Emit()
  onInputChange(e) {
      return e.target.value
  }
  @Emit()
  promise() {
      return new Promise(resolve => {
      setTimeout(() => {
          resolve(20)
      }, 0)
      })
  }
}

JS

export default {
  data() {
      return {
      count: 0
      }
  },
  methods: {
      addToCount(n) {
      this.count += n
      this.$emit('add-to-count', n)
      },
      resetCount() {
      this.count = 0
      this.$emit('reset')
      },
      returnValue() {
      this.$emit('return-value', 10)
      },
      onInputChange(e) {
      this.$emit('on-input-change', e.target.value, e)
      },
      promise() {
      const promise = new Promise(resolve => {
          setTimeout(() => {
          resolve(20)
          }, 0)
      })
      promise.then(value => {
          this.$emit('promise', value)
      })
      }
  }
 }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/61520.html

(0)
小半的头像小半

相关推荐

极客之家——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!