angular2-routing

Lazy loading module with APP_INITIALIZER

独自空忆成欢 提交于 2019-12-01 09:25:15
We have an application where we are lazy loading modules via router. We need to load a some resources before the app module starts, but we don't want to use resolve on router. That's why we are trying to create some provider factory (APP_INITIALIZER) to load some data from server and then use it in components in that lazy loaded module. So every module will have some different settings like translations and so on... I created a plunker for showcase. https://embed.plnkr.co/uBK23O5cA7QGwlvmbi73/ If you click on lazy it should wait 10 seconds to load the module but its not happening. We are using

End Observable interval when route changes in Angular

喜夏-厌秋 提交于 2019-12-01 09:14:33
I start a interval in an Angular component, but it keeps making requests even after I change the route. How can I stop the interval? //returns an observable getAllPolls() { return Observable.interval(2000).switchMap(() => this._http.get('https://xq4kftam4k.execute-api.us-east-1.amazonaws.com/test/polls2')) .map((res: Response) => res.json()) } You should save subscription to observable: this.subscription = getAllPolls().subscribe(...); And implement OnDestroy interface: ngOnDestroy() { this.subscription.unsubscribe(); } 来源: https://stackoverflow.com/questions/36582997/end-observable-interval

Angular 2 - Route guard not working on browser refresh

醉酒当歌 提交于 2019-12-01 07:56:52
问题 Having these routes defined in app.module.ts ... { path: 'mypath', component: MyComponent, canActivate: [RouteGuard] }, { path: '', redirectTo: '/home', pathMatch: 'full' }, { path: '**', redirectTo: '/home'} and this guard service import { Injectable } from '@angular/core'; import { CanActivate } from '@angular/router'; import { AngularFire } from 'angularfire2'; import { AngularFireAuth } from 'angularfire2'; @Injectable() export class RouteGuard implements CanActivate { private loggedIn:

Redirect to login route if the user not logged in Angular2 2.0.0-rc.4

六月ゝ 毕业季﹏ 提交于 2019-12-01 07:41:43
This is my html file <div class="container"> <h1>Dohatec Data</h1> <div class="navLinks"> <a [routerLink]="['/home']">Home</a>  <a [routerLink]="['/about']">About-Us </a>  <a [routerLink]="['/price']">Pricing</a> </div> </div> This is my RouterConfig. const routes: RouterConfig = [ { path: '', redirectTo: 'home', terminal: true }, { path: 'home', component: HomeComponent }, { path: 'about', component: AboutUsComponent }, { path: 'price', component: PriceComponent }, { path: 'login', component: LoginComponent } ]; I want to redirect the users if they are not loggedin in the /home route. Can I

Angular 2 new (RC1) router. Nested routes

守給你的承諾、 提交于 2019-12-01 07:36:05
In beta version from the child component (let's say 'child1' ) you could tell your parent to load (navigate to) a different child component instead of current by something like that: _router.parent.navigate(['child2']); ('_router' is injected in constructor) In RC1 there's no 'parent' property anymore so it seems like the you have to use _router.navigate() and provide the full url starting from the app root. Something like /root/grandparent/parent/child2 Now, the question is if the child component is only aware about parent's routes but not about grandparent - how do you do this? Would be nice

How to handle multiple queryParams in Angular2

别等时光非礼了梦想. 提交于 2019-12-01 06:44:39
I'm trying to implement a filtering mechanism in a new Angular2 app, that would allow me to filter a list of entries in an array. The entries may have around 20 properties that could be filtered on. I have so far created a list of filters in one component and then a list component that is routed to, as a child. I then planned to pass the filters as queryParams through the router. Starting with just one filter this was fine: On the send side I had: this.router.navigate(['/findlist'], {queryParams: {'g': myParam}}); The on the receive side, I had: this.subscription = this.route.queryParams

Lazy loading module with APP_INITIALIZER

£可爱£侵袭症+ 提交于 2019-12-01 05:54:59
问题 We have an application where we are lazy loading modules via router. We need to load a some resources before the app module starts, but we don't want to use resolve on router. That's why we are trying to create some provider factory (APP_INITIALIZER) to load some data from server and then use it in components in that lazy loaded module. So every module will have some different settings like translations and so on... I created a plunker for showcase. https://embed.plnkr.co/uBK23O5cA7QGwlvmbi73

Angular 2 new (RC1) router. Nested routes

那年仲夏 提交于 2019-12-01 05:18:39
问题 In beta version from the child component (let's say 'child1' ) you could tell your parent to load (navigate to) a different child component instead of current by something like that: _router.parent.navigate(['child2']); ('_router' is injected in constructor) In RC1 there's no 'parent' property anymore so it seems like the you have to use _router.navigate() and provide the full url starting from the app root. Something like /root/grandparent/parent/child2 Now, the question is if the child

Angular 2 Jasmine How to test a function of a component

浪尽此生 提交于 2019-12-01 05:12:56
How do I mock the click event of a function in Angular 2? For my Home Component I have: Home Component import { Component } from '@angular/core'; import { Router } from '@angular/router'; @Component({ moduleId: module.id, templateUrl: 'home.component.html', styleUrls: ['home.component.css'], selector: 'home', }) export class HomeComponent { constructor(private router: Router) { } redirectToUpload() { this.router.navigate(['/upload']); } redirectToAbout() { this.router.navigate(['/about']); } } Home Component spec import { ComponentFixture, TestBed, async } from '@angular/core/testing'; import

Redirect to login route if the user not logged in Angular2 2.0.0-rc.4

妖精的绣舞 提交于 2019-12-01 05:05:55
问题 This is my html file <div class="container"> <h1>Dohatec Data</h1> <div class="navLinks"> <a [routerLink]="['/home']">Home</a>  <a [routerLink]="['/about']">About-Us </a>  <a [routerLink]="['/price']">Pricing</a> </div> </div> This is my RouterConfig. const routes: RouterConfig = [ { path: '', redirectTo: 'home', terminal: true }, { path: 'home', component: HomeComponent }, { path: 'about', component: AboutUsComponent }, { path: 'price', component: PriceComponent }, { path: 'login', component