简体   繁体   English

路由器出口外部的Angular 2 Hide Component

[英]Angular 2 Hide Component outside router-outlet

My template looks like this 我的模板看起来像这样

<app-progress-bar></app-progress-bar>
<router-outlet></router-outlet>

I need to hide <app-progress-bar></app-progress-bar> when user is not logged in, I am using Guards to check if user is logged in, so when user is not logged in progress bar should be hidden. 当用户未登录时,我需要隐藏<app-progress-bar></app-progress-bar> ,我正在使用Guards检查用户是否已登录,因此当用户未登录时,进度条应隐藏。

 {
        path: 'login',
        component: LoginComponent,

    }

This route should hide <app-progress-bar></app-progress-bar> which is ProgressBarComponent 此路由应隐藏<app-progress-bar></app-progress-bar>这是ProgressBarComponent

Store user in a variable user. 将用户存储在变量用户中。 when user is login or value is true.. our progress bar will work.. when user is false or user is not login user variable have null. 当用户登录或值为true时。我们的进度条将起作用。当用户为false或用户未登录时,用户变量为null。 so will not be there. 所以不会在那里。

<app-progress-bar *ngIf="user" ></app-progress-bar>

also show us some code. 还向我们展示一些代码。 if you have some other issue. 如果您还有其他问题。 this problem you can easily solve using *ngIf structure directive. 您可以使用* ngIf结构指令轻松解决此问题。

using canActivate I hope this will help you 使用canActivate希望对您有帮助

for app-progress-bar component use canActivate: ['canActivateForLoggedIn'], 对于app-progress-bar组件,请使用canActivate: ['canActivateForLoggedIn'],

export const routes: Route[] = [{
    path: '',
    redirectTo: "login",
    pathMatch: "full"
}, 
{
    path: 'login',
    component: LoginComponent
}, 
{
    path: 'csvtemplate',
    component: TemplateComponent,
    canActivate: ['canActivateForLoggedIn'],
    children: [{
        path: '',
        redirectTo: 'dashboard'
    }, 
    {
        path:'dashboard',
        component: DashboardComponent
    },
    {
        path: 'csvtimeline/:month/:year',
        component: CsvTimelineComponent,
        canActivate: ['canActivateForLoggedIn'],
    },{
        path: 'adduser',
        component: adduserComponent
    }
    ]
}];

and in same routes file 并在相同的路由文件中

export const ROUTES_PROVIDERS = [{
  provide: 'canActivateForLoggedIn',
  useValue: () => !! Meteor.userId() <--- write your condition here.. i have used meteor.. i don't know what you have used
}];

after that in your module file import it and and put it in providers 之后,在模块文件中将其导入并放入提供程序中

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        ReactiveFormsModule,
        RouterModule.forRoot(routes),
    ],
    declarations: [
        AppComponent,
    ],
    providers: [
        ...ROUTES_PROVIDERS
    ],
    bootstrap: [
        AppComponent
    ]
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM