angular2-routing

How to broadcast events from app.component to router-outlet in angular2

天大地大妈咪最大 提交于 2019-11-30 15:37:13
I have created a service that has a stream$ that will emit an event, I hope after the app.component fires the stream$ emit, the child component in the <router-outlet></router-outlet> will be able to subscribe it, but now the problem I have is even if it subscribes it in the constructor, the subscription call back never gets fired. I wonder if this is different from when emitting events through a stream service? What is the best practice? The most simple way is to create event bus service: export class EventBusService { bus:Subject<Event> = new Subject<Event>(); dispatch(data:Event){ this.bus

How to lazy load a route as a child route/component

让人想犯罪 __ 提交于 2019-11-30 15:24:42
Let's take a look at my plunker: https://plnkr.co/edit/22RIoGsvzfw2y2ZTnazg?p=preview I want to lazy load a Module as a child-route. So the route /lazy should render the LazyModule into the <router-outlet> of my MainComponent . Then it will switch between that DummyComponent and the lazy loaded Module. Instead now that lazy loaded Module will be rendered into the AppComponent .. Any ideas? If you want to lazy load a module, do not import it as you've done here : src/app.ts : import { LazyModule } from './lazy/lazy.module'; ... @NgModule({ imports: [ BrowserModule, RouterModule.forRoot(routes),

Nesting Angular2 RC5 Routes Multiple Files

别来无恙 提交于 2019-11-30 14:59:09
问题 I'm trying to understand how I'm supposed to organize multiple routing files and make them all play nicely together. My desired routes look like this: / /forms /forms/:id /forms/named-form (named-form2, named-form3, etc.) /forms/named-form(2,3,etc)/admin /forms/named-form(2,3,etc)/admin/(several options here) My desired file organization: /app.module /app.routes /app.component <--Router Outlet /forms/forms.module /forms/forms.routes /forms/forms.component <--Router Outlet for displaying Form

Angular2: What's the equivalent of the router 'loadChildren' without lazy loading

旧巷老猫 提交于 2019-11-30 14:32:49
I'm trying 'plug' a sub ngModule (a feature module ) containing routing configurations (imported from RouterModule.forChild() ) into a parent ngModule. When using lazy loading, specifying where to 'plug' the child module is done using the loadChildren key in the parent module route configuration. ex: export const parentModuleRouteConfig = [{ path: 'myfeaturemodule', loadChildren: '/app/child.module' }]; Actually, i don't whant to use lazy loading . How can i tell the router to 'plug' (or use) the route config specified in a sub module at a given path ? With AOT Export your child routes instead

Angular2 Redirect After Login

时光怂恿深爱的人放手 提交于 2019-11-30 13:58:47
问题 I'm creating an authentication system in angular2 with the idea that if a user that is not authenticated tries to navigated to a "protected" url, the system will redirect the user to the login page putting in the url a query param called "next" that will help the login system redirect the user back to where he wanted to be in the first place. login?next=my-redirect-url To protect my components, I'm using the decorator @CanActivate(isUserAuthenticated) in all of them. The isUserAuthenticated

Angular2: Prevent authenticated users from accessing specific routes

家住魔仙堡 提交于 2019-11-30 13:49:41
问题 I've defined some routes in my main.ts file: const routes: RouterConfig = [ { path: '', component: HomeComponent }, { path: '', redirectTo: 'home', terminal: true }, { path: 'dashboard', component: DashboardComponent, canActivate: [LoggedInGuard] }, { path: 'login', component: LoginComponent }, { path: 'about', component: AboutComponent } ]; After successful login I want my authenticated users can be able to use specific routes (e.g. dashboard ). And without login they cannot access dashboard

Angular2 - two components with the same route

我的梦境 提交于 2019-11-30 13:39:09
In angularJS ui-router allows us to use 2 components with the same route using states (like in this example Angular UI Router: Different states with same URL? ). Is it possible to achieve the same behaviour in angular2? If it is could you link/provide some examples or workarounds? The use case here would be something similar to Facebook or Twitter where the URL stays the same but the content changes depending on if you are logged in or not. So far the only way I can think off to achieve this is using *ngIf in the 'parent' template to select selector of one of the two 'children' components.

Nesting Angular2 RC5 Routes Multiple Files

不羁的心 提交于 2019-11-30 13:33:05
I'm trying to understand how I'm supposed to organize multiple routing files and make them all play nicely together. My desired routes look like this: / /forms /forms/:id /forms/named-form (named-form2, named-form3, etc.) /forms/named-form(2,3,etc)/admin /forms/named-form(2,3,etc)/admin/(several options here) My desired file organization: /app.module /app.routes /app.component <--Router Outlet /forms/forms.module /forms/forms.routes /forms/forms.component <--Router Outlet for displaying Form Creation, Details, Settings, etc. /forms/named-form/named-form.module /forms/named-form/named-form

Angular 2: Inject a dependency into @CanActivate?

丶灬走出姿态 提交于 2019-11-30 12:43:52
In Angular 2, you can specify a @CanActivate annotation for a component where you can determine if the component should be activated or not. The reason it's not an interface is because the callback is called before the component is even instantiated. The problem is, I can't figure out a way to get dependencies injected into that callback. And I need my service that tells me whether I'm logged in or not (and as whom) to determine whether routing to a particular component is allowed or not. How can I inject a dependency into a @CanActivate callback? I'm using ES5, and this doesn't work: app

How to handle hash fragments from oauth redirect urls in Angular2 rc3 routing

倾然丶 夕夏残阳落幕 提交于 2019-11-30 12:14:57
I'm trying to find a way to handle setting up an Angular2 Typescript route (using the 3.0.0-alpha.8 router) that will handle routes that begin with hash fragments. The app I'm working on handles all login externally (something I have no control over) through a rails backend with oauth2. Redirecting users to the external login page works fine but when the redirect url, always some form of http://localhost:4200#access_token=TOKEN (where TOKEN is a series of numbers and letters) is sent back but I can't figure out how to set up a route that can handle the # sign so I can catch it and redirect to