angular2-routing

Versioning an Angular 2 App

旧街凉风 提交于 2019-12-05 02:47:11
问题 How can one mark versions of an Angular 2 app? There are some problems like views (via templateUrl ) are not getting updated in route views (child components). I've tried things like <script> System.config({ // baseURL: '/', packages: { app: { format: 'register', defaultExtension: 'js' } } }); System.import('v1/app/boot') .then(null, console.error.bind(console)); </script> Or app/v1/boot and playing with base but it's not working. Note: Just for the record, at development time, cached

How to test if a route exists in Angular2?

我只是一个虾纸丫 提交于 2019-12-05 02:20:21
In Angular2 how do I check if a route exists? I have a method that remembers which route to navigate to if the user is not authorised. On login I have: this.router.navigate([this.authService.redirectUrl]); But I only want to navigate IF the redirectUrl references a valid route, something like .. if (this.authService.redirectUrl is a valid route) { this.router.navigate([this.authService.redirectUrl]); } Is there any way to check this? You can check if the route exists like that using the promise way : this.router.navigate(['redirect']) .then(data => { console.log('Route exists, redirection is

angular2 guard not working on page refresh

时间秒杀一切 提交于 2019-12-05 02:00:10
问题 Before every request I want to be sure that there is a user profile available. I use a canActivateChild guard to do this. According to the documentation of angular2 it is possible to return an observable: https://angular.io/api/router/CanActivateChild app.routes.ts export const routes: Routes = [ { path: '', canActivateChild: [ProfileGuard], children: [ { path: 'home', component: HomeComponent, canActivate: [AuthGuard] }, { path: 'login', component: LoginComponent, canActivate: [GuestGuard] }

Configure history.pushState for Angular 2

回眸只為那壹抹淺笑 提交于 2019-12-05 01:46:22
问题 My Angular 2 application uses the default HTML 5 history.pushState location strategy. How do I configure a server with history.pushState so browser refreshes and browser URLs do not generate 404 s? For example, the Tour of Heroes application has a few different application routes like: http://localhost:8080/dashboard http://localhost:8080/heroes http://localhost:8080/detail/13 If you hit refresh on one of these routes, or type one of the URLs, you'll get 404 since /detail/13 is an application

Pass NavigationExtras to routerLink in template

不羁的心 提交于 2019-12-05 01:23:15
I want to disable changing the URL when routing in my Angular 2 app. I know that skipLocationChange: true has to be passed as a NavigationExtras parameter to the router. My question is: Is it possible to pass NavigationExtras to a routerLink from inside the template? What I have tried: <h1> {{title}} </h1> <ul> <li><a [routerLink]="['route1', { skipLocationChange: true }]">Route1</a></li> <li><a [routerLink]="['route2', { skipLocationChange: true }]">Route2</a></li> </ul> <router-outlet></router-outlet> but it does not work. After clicking a link, the route changes to http://localhost:4200

How to setup input bindings for a component when it's created by router-outlet?

跟風遠走 提交于 2019-12-05 01:07:35
When a component is created by the router how can I setup property bindings? It is not supported to use bindings for components added by the router. See also this discussion https://github.com/angular/angular/issues/4452 Use a shared service to communicate with components added by the router. For details see https://angular.io/docs/ts/latest/cookbook/component-communication.html 来源: https://stackoverflow.com/questions/34647918/how-to-setup-input-bindings-for-a-component-when-its-created-by-router-outlet

Access Parent @Component and vars from *Routed* Child Component

我的梦境 提交于 2019-12-05 01:06:52
I am trying to toggle a side nav menu, located at the top of my main App template using a button in a nested child component. I can't figure out how to get to the sidenav component in the parent to tell it to sidenav.open() . I know about @Input and @Output on a child component, but as I understand it, to use this I need to have some sort of DOM tag for the child component to attach these to? Such as: <app> <sidenav-component #sidenav>...</sidenav-component> <child [someInput]="some_parent_var" (childOpensNav)="sidenav.open()"></child> </app> Tons of articles on how to do this. Problem is that

Component undefined has no route config aka how to configure Angular 2 router for unit test?

China☆狼群 提交于 2019-12-05 00:47:27
问题 I'm working on setting up a specification for routing with Angular2. This is the app component: import {Component, View} from 'angular2/core'; import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router'; import {Search} from './search/search'; import {SearchResults} from './search-results/search-results'; @Component({ selector: 'my-app' }) @View({ template: `<div> <router-outlet></router-outlet> </div>`, directives: [ROUTER_DIRECTIVES] }) @RouteConfig([ {path: '/search', name: 'Search',

How to trigger route resolver manually

偶尔善良 提交于 2019-12-05 00:44:47
I'm resolving a user before accessing the account fragment of my user page : app-routing.component.ts { path: 'users/:id', component: UserComponent, resolve: {user: UsersService}, children: [ {path: '', pathMatch: 'full', redirectTo: 'account'}, {path: 'account', component: UserAccountComponent}, {path: 'sites', component: UserSitesComponent} ] } users.service.ts resolve(route: ActivatedRouteSnapshot, r: RouterStateSnapshot) { return this.getUser(route.params['id']); } user-account.component.ts ngOnInit() { this.route.parent.data.subscribe(data => { this.user = data['user']; // Initialize the

Angular2 relative route navigate in a guard

为君一笑 提交于 2019-12-05 00:19:54
We have child routes definitions presented here, in which we use a guard to check if the user has accepted terms before using our service. account/secret/secret.routes.ts: import { Routes } from '@angular/router'; import { SecretFormComponent } from './secret-form.component'; import { SecretTermsComponent } from './secret-terms.component'; import { TermsGuard } from './services/terms-guard.service'; export const secretRoutes: Routes = [ { path: '', redirectTo: 'form' }, { path: 'form', component: SecretFormComponent, canActivate: [TermsGuard] }, { path: 'terms', component: SecretTermsComponent