Javascript Ionic 2公共变量和内部函数
我对离子2有个问题,尤其是角离子。我的问题是,我有一个名为“isConnected”的变量,当我想从函数的内部函数访问时,我不能。它说不能定义未定义的属性。如何访问与内部函数连接的变量?我尝试了以下方法:BleProvider.prototype.isConnected,但不起作用。有人能给我解释一下这是怎么回事吗Javascript Ionic 2公共变量和内部函数,javascript,function,angular,typescript,ionic2,Javascript,Function,Angular,Typescript,Ionic2,我对离子2有个问题,尤其是角离子。我的问题是,我有一个名为“isConnected”的变量,当我想从函数的内部函数访问时,我不能。它说不能定义未定义的属性。如何访问与内部函数连接的变量?我尝试了以下方法:BleProvider.prototype.isConnected,但不起作用。有人能给我解释一下这是怎么回事吗 export class BleProvider { public isConnected = false; public mDevice; constructor(pub
export class BleProvider {
public isConnected = false;
public mDevice;
constructor(public http: Http) {}
connectToDevice(device){
console.log('Connecting to device...');
this.mDevice = device;
setTimeout(
ble.connectToDevice(
device,
onConnected,
onDisconnected,
onConnectError),
500);
function onConnected(device) {
console.log("Connected to device: " + device.name);
this.isConnected = true;
console.log("isConnected variable status: " + this.isConnected);
}
function onDisconnected(device) {
console.log('Disconnected from device: ' + device.name);
}
function onConnectError(error) {
console.log('Connect error: ' + error);
}
}
您应该像这样使用:
export class BleProvider {
public isConnected = false;
public mDevice;
constructor(public http: Http) { }
connectToDevice(device) {
console.log('Connecting to device...');
this.mDevice = device;
// Create the callbacks by using arrow functions () => {...}
let onConnected = (device) => {
console.log("Connected to device: " + device.name);
this.isConnected = true;
console.log("isConnected variable status: " + this.isConnected);
},
onDisconnected = (device) => {
console.log('Disconnected from device: ' + device.name);
},
onConnectError = (error) => {
console.log('Connect error: ' + error);
}
// You can also use an arrow function in the setTimeout! :)
setTimeout(() => {
ble.connectToDevice(device, onConnected, onDisconnected, onConnectError);
}, 500);
}
}
当使用常规函数时,this
关键字引用函数本身,但当使用箭头函数时,this
属性不会被覆盖,仍然引用组件实例(您在其中定义了isConnected
属性)。您应该这样使用:
export class BleProvider {
public isConnected = false;
public mDevice;
constructor(public http: Http) { }
connectToDevice(device) {
console.log('Connecting to device...');
this.mDevice = device;
// Create the callbacks by using arrow functions () => {...}
let onConnected = (device) => {
console.log("Connected to device: " + device.name);
this.isConnected = true;
console.log("isConnected variable status: " + this.isConnected);
},
onDisconnected = (device) => {
console.log('Disconnected from device: ' + device.name);
},
onConnectError = (error) => {
console.log('Connect error: ' + error);
}
// You can also use an arrow function in the setTimeout! :)
setTimeout(() => {
ble.connectToDevice(device, onConnected, onDisconnected, onConnectError);
}, 500);
}
}
当使用常规函数时,
this
关键字引用函数本身,但当使用箭头函数时,this
属性不会被覆盖,仍然引用组件实例(您在其中定义了isConnected
属性)。非常感谢您,你真的帮了我,澄清了我的想法。非常感谢你也许你也能回答这个问题:)。在相同的情况下,如果我想在this.isConnected=true之后调用外部函数
类似于this.myFunction()
(myFunction是在connectToDevice()
之外声明的,它说:this.myFunction()不是函数。非常感谢你,你真的帮助了我,澄清了我的想法。非常感谢你可能你也能回答这个问题:)。在相同的情况下,如果我想在this.isConnected=true之后调用外部函数
类似于this.myFunction()
(myFunction是在connectToDevice()
之外声明的,它说:this.myFunction()不是函数。