我有一个应用程序,其路由名为@Test
public void testSuccess() {
System.out.println(Mockito.anyObject()+"");
System.out.println(getRegistrationStatusManager.getRegistrationStatus(Mockito.anyObject()));
}
。
根据从API获取的值,我需要为上述路由加载不同的路由模块。举例说明:如果机会属于v2类型,则加载v2模块;如果其机会为v1,则加载v1模块。
之前:/opportunity/:id
app.routing.ts
我正在关注此博客以实现我的目标-https://medium.com/@german.quinteros/angular-use-the-same-route-path-for-different-modules-or-components-11db75cac455,这就是我现在的代码的样子。
现在: {
path: 'opportunity/:id',
loadChildren: () => import('./content/opportunity/opportunity.module').then(m => m.OpportunityModule)
},
{
path: 'opportunity-new/:id',
loadChildren: () => import('./content/opportunity-v2/opportunity-v2.module').then(m => m.OpportunityV2Module)
},
app.routing.ts
{
path: 'opportunity/:id',
loadChildren: () => import('./modules/opportunity-route-handler.module').then(m => m.OpportunityRouteHandlerModule)
}
:
opportunity-v2.module.ts
import { OpportunityService } from './../services/opportunity.service';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, ROUTES, Routes, ActivatedRoute } from '@angular/router';
@NgModule({
declarations: [],
imports: [
CommonModule,
RouterModule
],
providers: [
{
provide: ROUTES,
useFactory: configOppHandlerRoutes,
deps: [
OpportunityService,
ActivatedRoute
],
multi: true
}
]
})
export class OpportunityRouteHandlerModule {}
export function configOppHandlerRoutes(opportunityService: OpportunityService, router: ActivatedRoute) {
let routes: Routes = [];
opportunityService.checkOpportunityType().subscribe((data: boolean) => {
if (data) {
routes = [
{
path: '',
loadChildren: () => import('./../content/opportunity/opportunity.module').then(m => m.OpportunityModule),
resolve: { content: TranslationService }
}
];
} else {
routes = [
{
path: '',
loadChildren: () => import('./../content/opportunity-v2/opportunity-v2.module').then(m => m.OpportunityV2Module),
resolve: { content: TranslationService }
}
];
}
return routes;
});
}
:
opportunity.service.ts
现在我在这里遇到问题。
我无法使用import { map } from 'rxjs/operators';
import { ErrorService } from 'app/services/error.service';
import { AppApolloService } from 'app/services/app.apollo.service';
import { Injectable } from '@angular/core';
import { Router, ActivatedRoute, NavigationEnd, Event } from '@angular/router';
import { OpportunityQuery } from './opportunity.graphql';
@Injectable({
providedIn: 'root'
})
export class OpportunityService {
constructor(
private activatedRoute: ActivatedRoute,
private router: Router,
private appApollo: AppApolloService,
private errorService: ErrorService) {
}
public checkOpportunityType() {
// this.activatedRoute.paramMap.subscribe((params: any) => {
// console.log('params', params); // This returns empty params
// return true;
// });
// return this.activatedRoute.queryParamMap.subscribe(data => {
// console.log('data'); // This returns empty params
// });
return this.router.events.pipe(map(async (event: Event) => {
let currentUrl;
if (event instanceof NavigationEnd ) {
currentUrl = event.url; // value is '/opportunity/9432?apply=true'
let oppId = currentUrl.split('/')[2].split('?')[0]; // very messy but works. Need better solution.
return await this.getOpportunity(+oppId);
}
}));
}
getOpportunity(oppId) {
return this.appApollo.query(OpportunityQuery, {id: oppId})
.then((response: any) => {
const opportunity = response.getOpportunity;
if ((+opportunity.programme.id === 1) || (+opportunity.programme.id === 2) || (+opportunity.programme.id === 5)) {
return true;
} else {
return false;
}
}, err => {
this.errorService.error.next(true);
this.errorService.errorDetails.next(err.networkError);
});
}
}
或:id
从路线中获取ActivatedRoute
参数,这导致我不得不使用ActivatedRouteSnapshot
。有没有一种方法可以在不使用路由器事件的情况下获取路由参数?
如果有人能提出一种更好,更有效的方法来实现我的目标,我也将不胜感激。我发现了其他两种替代方法(https://medium.com/@lenseg1/loading-different-angular-modules-or-components-on-routes-with-same-path-2bb9ba4b6566),但是它们有自己的问题,例如router events
等,而我采用这种方法的原因是因为它看起来更干净。
答案 0 :(得分:-1)
取决于从API获取的值
在组件的构造函数中,您可以从API中获取值:
constructor(private route: Router) { }
根据值的方法,将您的路线网址放在下面:
this.route.navigateByUrl('/<set your url here>');