关于typescript:vuetypscript项目中遇到的问题

问题

  • typescript引入没有申明过的第三方库
  • typescript中应用map forEach filter find遍历用[]取属性时报错Element implicitly has an ‘any’ type because expression of type ‘string’ can’t b…

解答

1.申明第三方库

  • 有的第三方库是有申明文件的,这时咱们只须要
    npm install @types/{模块名}
  • 没有申明文件的第三方库

    • 在我的项目的src下建一个@type文件夹,在这个文件夹上来编写申明文件
    • 例子
// main.ts

import { VeRadarChart } from 've-charts'
Vue.component('VeRadarChart', VeRadarChart)
// @types/definition.d.ts
declare module 've-charts' {
  export class VeRadarChart {
  }
}

像这样申明一下就能用了

2.用[]获取属性报错

// index.vue
import Service from './service.ts'

const tService: any = Service

// Service不能间接被遍历获取属性,然而tService能够
let _promises = this.formItem.map(({service}: any): any => {
  return tService[service]()
})

// Promise.all...........xxxx......
// service.ts
export default {
  async getList(params) {
    return await XXXXXX
  }
}
// const.ts
type FormItem<T> = {
  label: T
  prop: T
  type: T
  service: T
}
export const formItem: Array<FormItem<any>> = [
  {
    label: '投诉类型',
    prop: 'complainType',
    type: 'select',
    service: 'getComplainType'
  },
  {
    label: '问题维度',
    prop: 'problemDim',
    type: 'select',
    service: 'getProblemDim'
  },
  {
    label: '用户危险等级',
    prop: 'riskLevel',
    type: 'select',
    service: 'getRiskLevel'
  }
]

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理