我在typescript中有一个服务类,在该类中我声明了一个公共字符串数组,但是当我尝试将其推入数组时,它将引发未定义的异常。我在这里怎么可能做错了?
无法读取未定义的属性'audioInput'
该类的源代码是:
export class AudioService {
public audioInput:Array<String>=[];
_navigator=<any> navigator;
constructor(){
}
getDevices(){
this._navigator.mediaDevices.enumerateDevices().then(this.gotDevices)
}
gotDevices(deviceInfos) : any {
for (let i = 0; i !== deviceInfos.length; ++i) {
const deviceInfo = deviceInfos[i];
let value = deviceInfo.deviceId;
if (deviceInfo.kind === 'audioinput') {
this.audioInput.push(deviceInfo.label);
}
}
}
}
答案 0 :(得分:-1)
如果您要在Cordova中访问“ this”。它指的是窗口对象。您需要定义让我= this;
export class AudioService {
public audioInput:Array<String>=[];
_navigator=<any> navigator;
constructor(){
}
getDevices(){
this._navigator.mediaDevices.enumerateDevices().then(this.gotDevices)
}
gotDevices(deviceInfos) : any {
let me = this.
for (let i = 0; i !== deviceInfos.length; ++i) {
const deviceInfo = deviceInfos[i];
let value = deviceInfo.deviceId;
if (deviceInfo.kind === 'audioinput') {
me.audioInput.push(deviceInfo.label);
}
}
}
}