angular2-routing

Angular2: Global Guard (user has to be logged in always)

半世苍凉 提交于 2019-12-17 16:20:56
问题 I'm building an application where there's no access at all for unauthenticated users. I wrote a LoggedInGuard , but now I have to add canActivate: [LoggedInGuard] to every route inside my router configuration (except the LoginComponent ). Is there a better way to get this working? My file / module layout looks like this: app/ AppModule AppRoutingModule AppComponent authentication/ AuthenticationModule AuthenticationRoutingModule LoginComponent contacts/ ContactsModule ContactsRoutingModule

Angular 2 different components with same route

谁说我不能喝 提交于 2019-12-17 14:47:00
问题 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

Want to prevent Component recreation while routing in Angular 2

本小妞迷上赌 提交于 2019-12-17 10:59:19
问题 Let's say we have two routes Dashboard and Profile . Dashboard has dynamic tabs feature like Google spreadsheet . I want to do some interactions(build charts, visualizing some data) creating tabs in the Dashboard . Now, if I route to Profile and then route back to Dashboard , I want to see what was before in those tabs in Dashboard . It means, I want to maintain the state in the client side. AFAIK while routing between components, it recreates components. Is it possible to make spreadsheet

Multiple canActivate guards all run when first fails

你。 提交于 2019-12-17 10:32:41
问题 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> {

Angular2 router, get route data from url, to display breadcrumbs

大兔子大兔子 提交于 2019-12-17 09:31:46
问题 I am using angular2 router. To draw the breadcrumb of an url, lets say site.com/a/b/c/15 I do the following: Get the route of site.com/a/b/c/15 and get the pretty name associated to the route Get the route of site.com/a/b/c and get the pretty name associated to the route Get the route of site.com/a/b and get the pretty name associated to the route Get the route of site.com/a and get the pretty name associated to the route So lets say I do have the following routes: { path: 'a', component: A,

Can't reload/refresh active route

半城伤御伤魂 提交于 2019-12-17 07:30:10
问题 I have recently updated to the new RC3 and Router3alpha and it seems some things have changed. I noticed that a click on the link of an active route does no longer result in the component to be reloaded. How do I achieve this behaviour with the new router3? My link looks like <a [routerLink]="['/link1']">Link1</a> And to test I simply used a random number in ngOnInit: export class LinkoneComponent implements OnInit { public foo: number; constructor() {} ngOnInit() { this.foo = Math.floor(Math

How do I detect user navigating back in Angular2?

夙愿已清 提交于 2019-12-17 05:49:10
问题 I have a component and I need to detect if user pressed back button in his browser to navigate back. Currently I'm subscribing router events. constructor(private router: Router, private activatedRoute: ActivatedRoute) { this.routerSubscription = router.events .subscribe(event => { // if (event.navigatesBack()) ... }); } I know that I can use window.onpopstate but it feels like a hack when using Angular2. 回答1: It's possible to use PlatformLocation which has onPopState listener. import {

Angular 2: getting RouteParams from parent component

心已入冬 提交于 2019-12-17 05:41:13
问题 How do I get the RouteParams from a parent component? App.ts : @Component({ ... }) @RouteConfig([ {path: '/', component: HomeComponent, as: 'Home'}, {path: '/:username/...', component: ParentComponent, as: 'Parent'} ]) export class HomeComponent { ... } And then, in the ParentComponent , I can easily get my username param and set the child routes. Parent.ts : @Component({ ... }) @RouteConfig([ { path: '/child-1', component: ChildOneComponent, as: 'ChildOne' }, { path: '/child-2', component:

Angular2 router 2.0.0 not reloading components when same url loaded with different parameters?

孤人 提交于 2019-12-17 04:32:39
问题 I have this in my .routing.ts file export const routing = RouterModule.forChild([ { path: 'page/:id', component: PageComponent }]); My PageComponent file checks the id parameter and loads the data accordingly. In previous versions of the router, if i went from /page/4 to /page/25, the page would "reload" and the components would update. Now when i try navigating to /page/X where X is the id, it only ever loads the first time, then the url changes, but the components do not "reload" again. Is

In Angular, how do you determine the active route?

China☆狼群 提交于 2019-12-17 03:45:25
问题 NOTE: There are many different answers here, and most have been valid at one time or another. The fact is that what works has changed a number of times as the Angular team has changed its Router. The Router 3.0 version that will eventually be the router in Angular breaks many of these solutions, but offers a very simple solution of its own. As of RC.3, the preferred solution is to use [routerLinkActive] as shown in this answer. In an Angular application (current in the 2.0.0-beta.0 release as