angular - Angular2 Routing from Navbar (AppComponent) -
i'm having issue routing in angular2 routing creates new object , keeps old object.
my navbar , [home] button resides in appcomponent.
.. app.html <a class="navbar-brand" [routerlink]="['auth']">home</a> <router-outlet></router-outlet> ..
the router defaults authcomponent @ start.
.. app.component.ts @routeconfig([ { path: '/', as: 'auth', component: authcomponent, useasdefault: true } ]) ..
i'll demonstrate scenario logging message in authcomponent's constructor.
.. auth.component.ts constructor(private _router: router) { console.log("constructing authcomponent"); this.router = _router; } ..
the response clicking home button produce constructing authcomponent
everytime clicked creating new object. (i can confirm old objects still exist)
i want able route somewhere , route authcomponent while still having access data set previously.
see plunker.
i'm using updated 2.0.0-beta.7
2.0.0.beta.9
you use canreuse
interface on component , routercanreuse
method. way, if method returns true
, same instance of component used within routing:
@component({ (...) }) class somecomponent implements canreuse { constructor(params: routeparams) { } routercanreuse(next: componentinstruction, prev: componentinstruction) { return true; } }
see link more details:
Comments
Post a Comment