angular2-routing

Multiple canActivate guards all run when first fails

故事扮演 提交于 2019-11-28 04:58:02
I have a route with two canActivate guards ( AuthGuard and RoleGuard ). The first ( AuthGuard ) checks to see if the user is logged in and, if not, redirects to the login page. The second checks to see if the user has a role defined that is allowed to view the page and, if not, redirects to the un-authorized page. canActivate: [ AuthGuard, RoleGuard ] ... export class AuthGuard implements CanActivate { canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> { ... this.router.navigate(['/login']); resolve(false); } export class RoleGuard implements CanActivate {

How to get parameter on Angular2 route in Angular way?

淺唱寂寞╮ 提交于 2019-11-28 04:49:38
Route const appRoutes: Routes = [ { path: '', redirectTo: '/companies/unionbank', pathMatch: 'full'}, { path: 'companies/:bank', component: BanksComponent }, { path: '**', redirectTo: '/companies/unionbank' } ] Component const NAVBAR = [ { name: 'Banks', submenu: [ { routelink: '/companies/unionbank', name: 'Union Bank' }, { routelink: '/companies/metrobank', name: 'Metro Bank' }, { routelink: '/companies/bdo', name: 'BDO' }, { routelink: '/companies/chinabank', name: 'China Bank' }, ] }, ... ] Example of link: http://localhost:8099/#/companies/bdo I want to get String bdo in the example link

ng2-translate not working in lazy-loaded module

£可爱£侵袭症+ 提交于 2019-11-28 04:22:16
问题 I'm using ng2-translate for language handling in an Angular 2 RC5 app I'm creating. The app has two feature modules, one lazy loaded and one eager loaded. TranslateModule is made available through a shared module. The problem is that the translate pipe works fine in the eager-loaded module but not the lazy-loaded one. To verify it has to do with the loading method I converted both to eager-loading and everything worked fine. A plunk that demonstrates the issue can be found here: Plunker The

How to set Bootstrap navbar “active” class in Angular 2?

那年仲夏 提交于 2019-11-28 04:17:11
How can I set Bootstrap navbar "active" class in Angular 2? I only found Angular 1 way . When I go to About page, add class="active" to About , and remove class="active" on Home . <ul class="nav navbar-nav"> <li class="active"><a [routerLink]="['Home']">Home</a></li> <li><a [routerLink]="['About']">About</a></li></li> </ul> Thanks If you use the new 3.0.0. component router ( https://github.com/angular/vladivostok ) you can use the routerLinkActive directive. No further javascript required. <ul class="nav navbar-nav"> <li [routerLinkActive]="['active']"> <a [routerLink]="['one']">One</a></li>

How to unit test a component that depends on parameters from ActivatedRoute?

梦想与她 提交于 2019-11-28 03:32:19
I am unit testing a component that is used to edit an object. The object has an unique id that is used in order to grab the specific object from an array of objects that are hosted in a service. The specific id is procured through a parameter that is passed via routing, specifically through the ActivatedRoute class. The constructor is as follows: constructor(private _router:Router, private _curRoute:ActivatedRoute, private _session:Session) { } ngOnInit() { this._curRoute.params.subscribe(params => { this.userId = params['id']; this.userObj = this._session.allUsers.filter(user => user.id

Angular 2 different components with same route

流过昼夜 提交于 2019-11-28 02:00:24
I have an application, which need to separate authenticated and guest users components. But I need, that both components will be loaded by '/' route. I wrote { path: 'desktop', loadChildren: 'app/member/member.module#MemberModule', canActivate: [LoggedInGuard], }, { path: '', loadChildren: 'app/guest/guest.module#GuestModule', canActivate: [GuestGuard], }, And it works. But how to make, that both component load by same url? I had tried to write path: '' for Member's module route, but the second router rule is not performed. Here are guards code: LoggedInGuard: canActivate(route:

How to use the activated route's data DIRECTLY in the HTML template containing the RouterOutlet?

时光毁灭记忆、已成空白 提交于 2019-11-28 01:28:35
This one took me a while to figure out, so I hope to save someone's time by sharing the solution in the SO's Q&A way. Here it goes: Goal I have an Angular8 web application, where I use the RouterModule to navigate between the components. Here is how my routes are defined: const routes: Routes = [ { path: 'moo', component: AppMooComponent, data: { title: 'Moo Section', desc: 'Moo is the first example component.' } }, { path: 'boo', component: AppBooComponent, data: { title: 'Boo Section', desc: 'Boo is the second example component.' } }, { path: 'foo', component: AppFooComponent, data: { title:

RC5 : Lazy loading of NgModule in different router-outlet

旧巷老猫 提交于 2019-11-28 01:22:44
问题 I'm using RC5's NgModule to do dynamic route loading. My app has two depth level. I have routes like : app/login app/dashboard/module1 app/dashboard/module2 etc... Each deph level has it's own router-outlet so I can define custom layout at each level. i.e. login and dashboard are displayed in app router-outlet while module1 and module2 are displayed in dashboard router-outlet. What is the configuration to load dynamically each route on demand ? 回答1: Here is a minimal working example of

return boolean instead of subscribe to canActivate

让人想犯罪 __ 提交于 2019-11-28 01:19:55
I have a component protected with canActivate guard. The Authguard in the checkLogin function subscribes from an observable but I do not know how to return a boolean value from it to canActivate. guard.service.ts canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { let url: string = state.url; return this.checkLogin(url); } public checkService:boolean; checkLogin(url: string):boolean { return this.loadAppSettings().subscribe(res=>{ console.log(this.checkservice); if (this.checkservice == true) { return true; } else{ this.router.navigate(['/error-user']); return

Hash Location Strategy in Angular 2

让人想犯罪 __ 提交于 2019-11-28 01:14:00
I'm trying to create an application with hash location strategy, but it does not add the hash to the url. For instance when I click on a button associated with { path: '/polls', name: 'Polls', component: PollsComponent } it loads the page with this url : localhost:3000/polls. What do I have to change to get the hash location strategy? Why do I have to set the default base url if I want to use hash location strategy? This is the routing in the app.component.ts where all the routing is defined: import {Component} from 'angular2/core' import {HTTP_PROVIDERS, Http} from 'angular2/http'; import