如何在服务中使用 Angular 路由器在组件之间导航?
How to use Angular Router inside a Service to navigate between components?
在我的 Ionic Angular 应用程序中,我在下面的 marker 服务 中添加了一个点击事件侦听器:
makeCapitalMarkers(map: L.map): void {
map.on('popupopen', function () {
if (document.querySelector('.norwayLink')) {
const link = document.querySelector('.norwayLink')
link.addEventListener('click', buttonClicked)
}
});
function buttonClicked() {
console.log('EXECUTED');
}
}
以上代码在我的home组件中调用:
ngAfterViewInit(): void {
this.markerService.makeCapitalMarkers(this.map);
}
目前正在打印"EXECUTED",所以方法正在运行.
但是当 buttonClicked() 被执行时,我想使用 angular 路由器导航到 chat 组件.
我试过下面的代码:
function buttonClicked() {
this.router.navigate(['/chat'])
}
Cannot read property 'navigate' of undefined
我在构造函数中将路由器声明为public,所以我不知道为什么它在这个函数中没有被拾取。
如何使用此功能导航至 聊天组件?
我应该能够在服务内导航路线,还是应该在 Home 组件 内完成?
这是我的路线:
const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', loadChildren: () => import('./home/home.module').then( m => m.HomePageModule)},
{
path: 'chat',
loadChildren: () => import('./chat/chat.module').then( m => m.ChatPageModule)
},
];
在function() { }
中调用this
指的是函数本身。而是在 class 上声明函数,并在箭头函数中调用它。
makeCapitalMarkers(map: L.map): void {
map.on('popupopen', () => {
if (!eventHandlerAssigned && document.querySelector('.norwayLink')) {
const link = document.querySelector('.norwayLink')
link.addEventListener('click', () => {
this.buttonClicked();
});
eventHandlerAssigned = true
}
});
}
buttonClicked() {
this.router.navigate(['/chat']);
}
你是这样在服务中注入Router
的吗?
import { Router } from '@angular/router';
...
constructor(private router: Router)
...
在我的 Ionic Angular 应用程序中,我在下面的 marker 服务 中添加了一个点击事件侦听器:
makeCapitalMarkers(map: L.map): void {
map.on('popupopen', function () {
if (document.querySelector('.norwayLink')) {
const link = document.querySelector('.norwayLink')
link.addEventListener('click', buttonClicked)
}
});
function buttonClicked() {
console.log('EXECUTED');
}
}
以上代码在我的home组件中调用:
ngAfterViewInit(): void {
this.markerService.makeCapitalMarkers(this.map);
}
目前正在打印"EXECUTED",所以方法正在运行.
但是当 buttonClicked() 被执行时,我想使用 angular 路由器导航到 chat 组件.
我试过下面的代码:
function buttonClicked() {
this.router.navigate(['/chat'])
}
Cannot read property 'navigate' of undefined
我在构造函数中将路由器声明为public,所以我不知道为什么它在这个函数中没有被拾取。
如何使用此功能导航至 聊天组件?
我应该能够在服务内导航路线,还是应该在 Home 组件 内完成?
这是我的路线:
const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', loadChildren: () => import('./home/home.module').then( m => m.HomePageModule)},
{
path: 'chat',
loadChildren: () => import('./chat/chat.module').then( m => m.ChatPageModule)
},
];
在function() { }
中调用this
指的是函数本身。而是在 class 上声明函数,并在箭头函数中调用它。
makeCapitalMarkers(map: L.map): void {
map.on('popupopen', () => {
if (!eventHandlerAssigned && document.querySelector('.norwayLink')) {
const link = document.querySelector('.norwayLink')
link.addEventListener('click', () => {
this.buttonClicked();
});
eventHandlerAssigned = true
}
});
}
buttonClicked() {
this.router.navigate(['/chat']);
}
你是这样在服务中注入Router
的吗?
import { Router } from '@angular/router';
...
constructor(private router: Router)
...