vue 初试 typescript

16 篇文章 1 订阅
2 篇文章 0 订阅

vue 里使用 ts 需要另外装两个包

  "dependencies": {
    "vue-class-component": "^7.2.2",    //  这个是 写 vue 的官方库
    "vue-property-decorator": "^8.3.0",   //  这个对vue-class-component 做了一层封装
    "vuex-class": "^0.3.2"   // 这个是为了 vuex 的
  },

1、明确赋值断言

<script lang="ts">
import { Stock } from '@/store/state'
import { Component, Vue, Prop } from 'vue-property-decorator'
@Component({})
export default class SGird extends Vue {
  @Prop({
    type: Array,
    default: () => []
  })
  options: Stock[]
}
</script>

当代码这样写的时候,编辑器会提示错误

就是说你没有给这个只进行初始化,但是事实上,在上面的 @prop 装时器中已经进行初始化了

所以需要使用

@Prop({
  type: Array,
  default: () => []
})
options!: Stock[]

在冒号前面加上   !   明确赋值断言,告诉 编辑器,这个已经被赋值了

2、类型断言

在使用 computed 的时候,这个是一个很常见的需求,返回一个布尔值

  get showPopper(): boolean {
    return (this.showList && this.inputV && this.options.length > 0)
  }

但是这样同样会报错,这个时候就需要进行类型断言了,就是强行将这个返回值的类型指向  布尔值

  get showPopper(): boolean {
    return (this.showList && this.inputV && this.options.length > 0) as boolean
  }

3、监听的时候,需要第一时间执行

在vue里,原本加一个 immediate 就行,但是现在使用了 Watch 的装饰器之后,写法就变了

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

@Watch('value', { immediate: true })
  handleValueChange(n: number | string): void {
    if (n !== this.inputV) {
      this.inputV = n
    }
  }

4、使用 debounce

以前的代码直接使用 debounce 包装一下自己的函数,这个函数就是防抖的了,但是现在换一种写法

import { Bind, Debounce } from 'lodash-decorators'  // 导入 lodash包
....

  @Bind()
  @Debounce(300)
  handleInput(val: string): void {
    if (val) {
      this.$store.dispatch('getStockList', {
        payload: { nameOrCode: val }
      })
    } else {
      this.$store.commit('SET_STOCK_LIST', [])
    }
  }

5、event 事件

在 vue @input 之类的事件中,会传递一个 event 属性,当时 一直 以为是 Vue 的内部属性,直到去看了文档

原话是 原生的 DOM 事件,这样的话,反而就简单了,直接使用 Event 作为 类型

6、最后讲一下 vuex 的使用

store/state.ts 文件

export interface Option {
  label: string
  value: string | number
  [propName: string]: string | number
}

export interface State {
  seasonList: Array<Option>
}

export const state: State = {
  seasonList: [],
}

store/index.ts文件

import Vue from 'vue'
import Vuex from 'vuex'
import { state, State, Option } from './state'
import api from '@/api/api.js'

Vue.use(Vuex)

export default new Vuex.Store({
  state,
  mutations: {
    SET_SEASON_LIST(state: State, list: Array<Option>) {
      state.seasonList = list
    },
  },
  actions: {
    async getReportList({ commit }) {
      const {
        data: { code, data }
      } = await api.search.reportList({})
      if (code === 200) {
        const list = data.map((d: ReportList) => ({
          label: d.report,
          value: d.date
        }))
        commit('SET_SEASON_LIST', list)
      }
    },
  },
  modules: {}
})

Home.vue文件

<script lang="ts">
import { State } from 'vuex-class'
import { Option } from '@/store/state'
import { Bind, Debounce } from 'lodash-decorators'

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

@Component({
  components: {
  }
})
export default class Home extends Vue {
  created() {
    this.$store.dispatch('getReportList')
  }

  @State private seasonList!: Option[]

}
</script>

7、使用 vuex 的模块化

同上面的代码

store/state.ts 文件

export interface Option {
  label: string
  value: string | number
  [propName: string]: string | number
}

export interface ReportList {
  date: string
  report: string
}

/**
* 增加的这个参数,就是为了 给 actions 传参的
*/
export interface Payload {
  payload: any
  callback?: (data?: any) => void
}

export interface State {
  seasonList: Array<Option>
}

export const state: State = {
  seasonList: [],
}

store/index.ts文件

import Vue from 'vue'
import Vuex from 'vuex'
import search from './modules/search'

Vue.use(Vuex)

export default new Vuex.Store({
  mutations: {},
  actions: {},
  modules: {
    search
  }
})

store/modules/search.ts文件

import { state, State, Option, Payload } from '../state'
import { ActionContext } from 'vuex'
import api from '@/api/api.js'

const search = {
  namespaced: true,
  state: state,
  mutations: {
    SET_SEASON_LIST(state: State, list: Array<Option>) {
      state.seasonList = list
    },
  },
  actions: {
    async getReportList(
      // 这个类型就是 整个参数的类型,要注意的是
      // 传进去的两个泛型一个 一个是当前节点的state,一个是 根节点的 state, 
      // 这里为了方便,就是同一个了      ActionContext<State, RootState>
      { commit }: ActionContext<State, State>,   
      // 这里就是用了自定义的 参数类型
      { payload, callback }: Payload
    ) {
      const {
        data: { code, data }
      } = await api.search.reportList(payload)
      if (code === 200) {
        commit('SET_SEASON_LIST', data)
        callback && callback(data)
      }
    }
  }
}

export default search

  这里是 vuex 里面关于 ActionContext 的定义

关于调用:

Home.vue文件

<script lang="ts">
import { State } from 'vuex-class'
import { Option } from '@/store/state'
import { Bind, Debounce } from 'lodash-decorators'

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

@Component({
  components: {
  }
})
export default class Home extends Vue {
  created() {
    this.$store.dispatch('search/getReportList')
  }

  // 在 state 里面就 实现了 模块化的提取
  // 在这个时候 private seasonList!: Option[] 这里的 
  // seasonList 名字也不是固定的,甚至可以叫做 list 等等
  @State(state => state.search.seasonList) private seasonList!: Option[]

}
</script>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue.js 和 TypeScript 是两个非常流行的前端开发工具和编程语言,它们可以很好地结合在一起,为构建大型、可维护的单页面应用(SPA)提供强大的支持。 **Vue.js**: - Vue 是一款轻量级的 JavaScript 框架,以其简单易学、组件化开发和响应式数据绑定而知名。 - 它提供了视图层(View)、模板语法、指令(Directives)和响应式系统,使得开发者能够快速构建用户界面。 - Vue 2.x版本引入了Vuex(状态管理)和Vue Router(路由管理),进一步增强了应用的组织和扩展能力。 **TypeScript**: - TypeScript 是由 Microsoft 开发的一种开放源代码的超集 JavaScript,它添加了静态类型检查、接口、类等面向对象的特性,使JavaScript代码更易于维护和扩展。 - 在 Vue 中使用 TypeScript,可以帮助开发者避免类型错误,提高代码质量和可读性,同时 TypeScript 支持强类型定义,方便团队协作。 将 VueTypeScript 结合的优势包括: 1. **类型安全**:TypeScript 提供的类型注解可以在编译期间捕捉潜在的错误,使代码更健壮。 2. **代码提示**:IDE 集成 TypeScript 支持后,开发者可以得到更好的代码提示和自动完成功能。 3. **大型项目管理**:对于大型项目,TypeScript 可以更好地支持模块化、类和接口,提高代码结构的清晰度。 4. **团队协作**:TypeScript 更容易理解和维护,有助于团队成员之间的沟通和协作。 **相关问题--:** 1. 在实际项目中,如何在 Vue 中启用 TypeScript 以及配置步骤是什么? 2. 类型安全如何帮助 Vue 项目的调试和维护? 3. Vue 的组件和 TypeScript 类有什么相似之处?

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值