Angular2子组件浏览器刷新重定向到父组件

时间:2016-08-22 10:04:18

标签: angular typescript angular2-routing

我的应用程序有2个父组件。

@Component({
  selector: 'my-app',
    directives: [ROUTER_DIRECTIVES],
  template: `
    <router-outlet></router-outlet>
`,
    providers: [ROUTER_PROVIDERS]
})

@RouteConfig([
    { path : '/router/...', name:'Router', component: RoutingComponent, useAsDefault: true},
    { path : '/login', name:'Login', component: LoginComponent }
])

export class AppComponent implements OnInit{

    constructor(
        private _router: Router
    ){}

    ngOnInit(){
        if(localStorage.getItem("user")=== null) {
            this._router.navigate(['Login']);
        }
        else{
            console.log(" APP COMPONENT");
            this._router.navigate(['/Router', 'RegisterClient']);
        }
    }
}

当用户登录时,应用程序会重定向到Router的子组件,名为RegisterClient,因为它是应用程序的主页面。 如果用户未登录,则应用程序将重定向到LoginComponent。

我的路由器组件有许多子组件

@Component({
selector:'router',
directives: [ROUTER_DIRECTIVES],
templateUrl: 'app/routing.component.html',
providers: [AuthenticationService, MessageService]

})

@RouteConfig([
{ path : 'registerClient', name:'RegisterClient', component: RegisterClientComponent},
{ path : 'searchClient', name:'SearchClient', component: SearchClientComponent},
{ path : 'updateClient/:id', name:'UpdateClient', component: UpdateClientComponent},
{ path : 'registerMerchandise', name:'RegistMerc', component: RegisterMercComponent},
{ path : 'searchMerchandise', name:'SearchMerc', component: SearchMerchandiseComponent},
{ path : 'updateMerchandise/:id', name:'UpdateMerc', component: UpdateMercComponent},
{ path : 'registMaterial', name:'RegistMaterial', component: RegisterMaterialComponent},
{ path : 'searchMaterial', name:'SearchMaterial', component: SearchMaterialComponent},
{ path : 'updateMaterial/:id', name:'UpdateMat', component: UpdateMaterialComponent},
{ path : 'messages', name:'MessagesComponent', component: MessagesComponent}

])

问题是,当我在任何子组件中,并尝试使用F5或浏览器按钮刷新页面时,页面将刷新并始终重定向到RegisterClient组件,并且我想刷新子组件和留在同一页。

任何人都可以提供帮助吗?

1 个答案:

答案 0 :(得分:0)

您已经在使用localstorage。

在刷新父ID后,将ID放入localstorage中,然后切换到右侧页面。