Angular 在 cordova 内部调用成功回调不起作用
Angular Call inside cordova success Callback doesn't work
我正在 运行使用 Angular 开发一个 Ionic 应用程序。我正在使用 Ionic 的 Diagnostic 插件,但我使用的方法之一不适用于 Diagnostic 插件,所以我使用 cordova.plugins.diagnostic.
https://ionicframework.com/docs/native/diagnostic
https://github.com/dpa99c/cordova-diagnostic-plugin
我的代码是这样的:
onClickEnable() {
cordova.plugins.diagnostic.requestRemoteNotificationsAuthorization({
successCallback() {
console.log('Successfully requested remote notifications authorization');
// HERE I WOULD LIKE TO DO A THIS.ROUTER.NAVIGATE(['/page']);
},
errorCallback(err) {
console.error('Error requesting remote notifications authorization: ' + err);
},
types: [
// some value
],
omitRegistration: false,
});
}
当我 运行 应用程序时,我的弹出窗口显示,我得到 console.log
但我想要的是,在用户选中弹出窗口中的一个选项后,它会导航到另一个页面。
所以我试图在成功回调中放入一个 this.router.navigate(['/page]);
,但它不起作用。
编辑:像这样,不起作用
onClickEnable() {
cordova.plugins.diagnostic.requestRemoteNotificationsAuthorization({
successCallback: () => {
console.log('Successfully requested remote notifications authorization');
this.router.navigate(['/page']);
},
errorCallback: (err) => {
console.error('Error requesting remote notifications authorization: ' + err);
}
我知道我用错了。有人可以解释我该怎么做吗?我也试过在方法后面加一个.then()...
,但是不行。
如果我在 requestRemoteNotificationsAuthorization
方法结束后调用 router.navigate
,它会导航到页面,即使用户没有触摸弹出窗口。
查看 requestRemoteNotificationsAuthorization(params)
函数的文档,您似乎可以将回调作为内联箭头函数发送,以引用封闭 class 范围内的 this
关键字。
尝试以下方法
onClickEnable() {
cordova.plugins.diagnostic.requestRemoteNotificationsAuthorization({
successCallback: () => {
console.log('Successfully requested remote notifications authorization');
this.router.navigate(['/page']);
},
errorCallback: (err) => {
console.error('Error requesting remote notifications authorization: ' + err);
},
types: [
// some value
],
omitRegistration: false,
});
}
要详细了解 Javascript 中的 this
关键字以及如何在回调函数中访问它,您可以参考 here.
实际上,我认为在 cordova successcallback 中,我处于与 Angular 组件不同的上下文中,这就是为什么我的 IDE 没有给我完成的答案
设置 const that = this
后,使用that.router.navigate
,识别为:
所以我的代码现在看起来像这样,它解决了我的问题:
onClickEnable() {
const that = this;
cordova.plugins.diagnostic.requestRemoteNotificationsAuthorization({
successCallback: () => {
console.log('Successfully requested remote notifications authorization');
that.router.navigate(['/page']);
},
errorCallback: (err) => {
console.error('Error requesting remote notifications authorization: ' + err);
},
types: [
// some value
],
omitRegistration: false,
});
}
我正在 运行使用 Angular 开发一个 Ionic 应用程序。我正在使用 Ionic 的 Diagnostic 插件,但我使用的方法之一不适用于 Diagnostic 插件,所以我使用 cordova.plugins.diagnostic.
https://ionicframework.com/docs/native/diagnostic
https://github.com/dpa99c/cordova-diagnostic-plugin
我的代码是这样的:
onClickEnable() {
cordova.plugins.diagnostic.requestRemoteNotificationsAuthorization({
successCallback() {
console.log('Successfully requested remote notifications authorization');
// HERE I WOULD LIKE TO DO A THIS.ROUTER.NAVIGATE(['/page']);
},
errorCallback(err) {
console.error('Error requesting remote notifications authorization: ' + err);
},
types: [
// some value
],
omitRegistration: false,
});
}
当我 运行 应用程序时,我的弹出窗口显示,我得到 console.log
但我想要的是,在用户选中弹出窗口中的一个选项后,它会导航到另一个页面。
所以我试图在成功回调中放入一个 this.router.navigate(['/page]);
,但它不起作用。
编辑:像这样,不起作用
onClickEnable() {
cordova.plugins.diagnostic.requestRemoteNotificationsAuthorization({
successCallback: () => {
console.log('Successfully requested remote notifications authorization');
this.router.navigate(['/page']);
},
errorCallback: (err) => {
console.error('Error requesting remote notifications authorization: ' + err);
}
我知道我用错了。有人可以解释我该怎么做吗?我也试过在方法后面加一个.then()...
,但是不行。
如果我在 requestRemoteNotificationsAuthorization
方法结束后调用 router.navigate
,它会导航到页面,即使用户没有触摸弹出窗口。
查看 requestRemoteNotificationsAuthorization(params)
函数的文档,您似乎可以将回调作为内联箭头函数发送,以引用封闭 class 范围内的 this
关键字。
尝试以下方法
onClickEnable() {
cordova.plugins.diagnostic.requestRemoteNotificationsAuthorization({
successCallback: () => {
console.log('Successfully requested remote notifications authorization');
this.router.navigate(['/page']);
},
errorCallback: (err) => {
console.error('Error requesting remote notifications authorization: ' + err);
},
types: [
// some value
],
omitRegistration: false,
});
}
要详细了解 Javascript 中的 this
关键字以及如何在回调函数中访问它,您可以参考 here.
实际上,我认为在 cordova successcallback 中,我处于与 Angular 组件不同的上下文中,这就是为什么我的 IDE 没有给我完成的答案
设置 const that = this
后,使用that.router.navigate
,识别为:
所以我的代码现在看起来像这样,它解决了我的问题:
onClickEnable() {
const that = this;
cordova.plugins.diagnostic.requestRemoteNotificationsAuthorization({
successCallback: () => {
console.log('Successfully requested remote notifications authorization');
that.router.navigate(['/page']);
},
errorCallback: (err) => {
console.error('Error requesting remote notifications authorization: ' + err);
},
types: [
// some value
],
omitRegistration: false,
});
}