Typescript + Vue + Eslint使用不报错的方法总结。
一、vue-class-component官方文档里的一种解决办法
public $refs!: { projectCreate: ProjectCreate };
this.$refs.projectCreate.open();
public $refs!: { input: HTMLInputElement };
this.$refs.input.focus();
二、vuex-class 用法
@State private setting!: SettingState; @State nav!: { homePath: '' };
@Getter private syncData: any;
@Mutation private changeHourly!: (checked: boolean) => void;
@Action private sync!: (data: any) => void;
三、ts提示语总结
对于刚刚接触ts的鸟来说这些提示也是无比的陌生。
比如:
- file should end with a newline (eofline): 文件应该以空行结尾,在ts的代码最后打一个空行就行了。
- Missing semicolon (semicolon): 缺少分号, 在ts的语句后指定位置加上分号即可。
- missing whitespace (whitespace): 缺少空格,在相应的位置打上空格即可。
四、类构造函数对象参数不使用any,同时继承父类和接口的办法
interface FormTableInterface {
getTableData(params?: any): any;
postFormData(params: any): any;
putFormData(params: any): any;
deleteRecord(ids: number[] | string[]): any;
}
class BaseBusinessDepartment extends CmdbApiClass implements FormTableInterface {
id = 0;
company = '';
department = '';
readonly apiType = 'business_department';
constructor(data: Partial<BaseBusinessDepartment> = {}) {
super();
Object.assign(this, data);
}
}
五、对象数组的初始化
const projects: Array<ProjectClass> = [
new ProjectClass({ name: '项目长花木成畦手自栽花木成畦手自栽花木成畦手自栽', icon: 'el-icon-cpu' }),
new ProjectClass({ name: '项目短', start: true, icon: 'el-icon-link' }),
new ProjectClass({ name: '项目短3', visitDate: new Date('2021/03/20'), icon: 'el-icon-connection' }),
new ProjectClass({ name: '项目短3', icon: 'el-icon-medal-1' }),
new ProjectClass({ name: '项目短3', icon: 'el-icon-trophy' })
];