angular2-routing

Angular2 trigger CanActivate while page is active

人走茶凉 提交于 2019-12-07 07:11:35
问题 As far as I understand it the CanActivate interface in Angular2 is only effective on route changes. So if I have a page p that requires the user to be logged in I can create a guard that implements the CanActivate interface which will allow the RouterModule to activate p only if the user is logged in. But what happens when p is active and the user gets logged out? I have tried to find best practices on checking if a page is allowed to be active without changing the route in the RouterModule

Cancel Route Navigation from Resolver in Angular 2

限于喜欢 提交于 2019-12-07 06:20:13
问题 I needed to cancel the route on promise rejection and stay on the current page without redirecting to default/error page, I have tried different ways of rejecting the promise but ended up resolving the route or redirected to the default route. @Injectable() export class ModelResolver implements Resolve<MyModel> { constructor(private router: Router) { } resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<any> { let params: any = route.params; let model: MyModel= new

Angular 2 - Reload component when routerLink clicked again

别来无恙 提交于 2019-12-07 04:56:53
问题 I am working on Angular 2 project with following file structure. HeaderComponent.ts AppComponent.ts Page1Component.ts Page2Component.ts I have following template in my HeaderComponent.ts <nav class="navbar navbar-default navbar-fixed-top" role="navigation"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"><

Getting Can't bind to 'routerLink' since it isn't a known property of 'a'. error in spite of referencing router moudule

喜欢而已 提交于 2019-12-07 04:48:30
问题 I am implementing basic routing in my angular 4 application and getting the following error when loading the application on the browser. I have defined the routes in approuting.module as also referenced the router module in Ngmodule as well approuting.module. Not sure what the problem is Can't bind to 'routerLink' since it isn't a known property of 'a'. Can't bind to 'routerLink' since it isn't a known property of 'a'. ("ew" [hidden]="!dataItem.isVisibleView"> <a [ERROR ->][routerLink]="['

angular 2 update app component from router outlet component

自作多情 提交于 2019-12-07 04:46:20
问题 I'm using angular 2 rc1 and the new router @angular/router In app component I have a nav section and router outlet <nav> <a *ngIf="auth?.userName == null" [routerLink]="['/login']">Login</a> <a *ngIf="auth?.userName != null" (click)="logout()">Logout</a> {{auth?.userName}} </nav> <router-outlet></router-outlet> I inject a loginService into app component and subscribe to an event in ngOnInit ngOnInit() { this.loginService.loginSuccess.subscribe(this.loginSuccess); } ngOnDestroy() { this

Installing Angular 2 RC2 w/ new component router

余生颓废 提交于 2019-12-07 04:18:40
问题 I'm probably missing something simple, but trying to use the new Component Router I can't get VS Code to recognize it. I changed all my @angular references in package.json to 2.0.0-rc.2 ; ran npm install and VS Code doesn't like my imports: When I navigate to the @angular/router folder in node_modules , and inspect the index.d.ts I see all the references for the RC1 router. Removed the @angular folder and installed again, same result. 回答1: For the new release of angular rc2. The version of

How to skip a page in browser history when navigating back?

允我心安 提交于 2019-12-07 04:14:03
问题 I have an Angular 2 app with the Router. Let's say the user is on a page A in the app, he then navigates to a page B and then to a page C. At this moment when he clicks the Back button on the browser I want him to get back to the page A (by skipping B). How can I achieve it? 回答1: When navigating from B to C one has to set { replaceUrl: true }: this.router.navigate(["/c"], { replaceUrl: true }); This replaces the page B by C in the browser history. 回答2: Quick and easy solution is Use

How do you attach an EventEmitter Output to a component under routing?

眉间皱痕 提交于 2019-12-07 03:27:22
问题 I have a route config @RouteConfig([ { path: '/', name: 'Start', component: Journal }, { path: '/register', name: 'Register', component: Register }, { path: '/login', name: 'Login', component: Login }, { path: '/settings', name: 'Settings', component: Settings }, ]) I'd like to listen to events emitted from Journal's @Output. How can I do that when the only reference I have in the template is <a class="nav-item nav-link" [routerLink]="['Journal']">Journal</a> ? 回答1: I realize this is pretty

Angular 2: Route generator for '…' was not included in parameters passed

孤人 提交于 2019-12-07 03:03:42
问题 So I've got the following piece of code inside my AuthenticationService. After login credentials are checked, the user get's redirected to their profile: authentication.service.ts : redirectUser(username:string):void { // Redirect user to profile on successful login this.router.navigate(['../Profile/Feed', {username: username}]); } And it all worked fine, but ever since I introduced some child routes inside the ProfileComponent , I ran into some errors. First of all, this is my AppComponent

Angular2 Safari Back Button

ⅰ亾dé卋堺 提交于 2019-12-07 03:02:11
问题 In the actual Angular2 beta 14 (and before) there seems to be an issue with the back button (when using routing, and several views) on Safari (actually using 9.1): https://github.com/angular/angular/issues/7722 I also experienced this problem, while this works fine e.g. on Chrome. I'm looking for a workaround until the issue is fixed? 回答1: On "angular2": "2.0.0-beta.14" I had to run the tick inside a zone to get it to work for me. import {ApplicationRef, <anything else you need>} from