Ionic 2:无法从内部功能导航
Ionic 2: Unable to navigate from inner function
我试图通过点击 google 地图标记导航到一个页面。我可以在内部函数之外执行此操作(仅限 Inside initializeMap 函数),只是在内部函数中执行时遇到问题。
这是我的构造函数:
static get parameters() {
return [[NavController],[Platform]];
}
constructor(navController, platform, app) {
this.navController = navController;
this.platform = platform;
this.app = app;
this.map = null;
this.markers = [];
platform.ready().then(() => {
this.initializeMap();
});
}
在我的 initializeMap 方法中包含 populateLocks 方法:
function populateLocks(auth,unauth,map){
for (var k in auth) {
(function (id) {
var auth = new google.maps.Marker({
icon: authImage,
map: map,
position: authLocks[id].latLng
});
google.maps.event.addListener(auth, 'click', function () {
//alert(auth[id].id);
this.navController.push(SettingsPage);
});
})
(k)
}
使用 this.navController 或 navController 会抛出如下错误:
for this.navController.push
对于 navController.push
要么使用 () =>
而不是 function ()
更多信息:https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Functions/Arrow_functions
或在函数外使用 var self = this;
并引用 self.navController
而不是 this.navController
,如下所示:
var self = this;
google.maps.event.addListener(auth, 'click', function () {
//alert(auth[id].id);
self.navController.push(SettingsPage);
});
我试图通过点击 google 地图标记导航到一个页面。我可以在内部函数之外执行此操作(仅限 Inside initializeMap 函数),只是在内部函数中执行时遇到问题。
这是我的构造函数:
static get parameters() {
return [[NavController],[Platform]];
}
constructor(navController, platform, app) {
this.navController = navController;
this.platform = platform;
this.app = app;
this.map = null;
this.markers = [];
platform.ready().then(() => {
this.initializeMap();
});
}
在我的 initializeMap 方法中包含 populateLocks 方法:
function populateLocks(auth,unauth,map){
for (var k in auth) {
(function (id) {
var auth = new google.maps.Marker({
icon: authImage,
map: map,
position: authLocks[id].latLng
});
google.maps.event.addListener(auth, 'click', function () {
//alert(auth[id].id);
this.navController.push(SettingsPage);
});
})
(k)
}
使用 this.navController 或 navController 会抛出如下错误:
for this.navController.push
对于 navController.push
要么使用 () =>
而不是 function ()
更多信息:https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Functions/Arrow_functions
或在函数外使用 var self = this;
并引用 self.navController
而不是 this.navController
,如下所示:
var self = this;
google.maps.event.addListener(auth, 'click', function () {
//alert(auth[id].id);
self.navController.push(SettingsPage);
});