问题描述:在ts中,使用对象key取值时,会出现如下警告,ts报错提示:TS7053: Element implicitly has an ‘any’ type because expression of type ‘string’ can’t be used to index type ‘TempObj’. No index signature with a parameter of type ‘string’ was found on type ‘TempObj’.,如下图所示
解决方案:将对象的接口类型声明改为如下声明即可
let tempObj: {[index: string]:any} = {
dashboard: 'dashboard',
mySetting: 'mySetting',
}
let fullPathName: string = route?.fullPath.substring(1)
resultStr = tempObj[fullPathName]