2

In @NgModule , how to open different component due to something like user type?

I have a NgModule , have two different pages, and I want to open different page due to the user type

@NgModule({
imports: [ SharedModule ,RouterModule.forChild([
{ path: 'edit', component: EditComponent },
{ path: 'view', component: ViewComponent }
])],

declarations: [
EditComponent, ViewComponent
],

})
export default class MainModule { 
constructor(private u:UserService){
   //------

   //------
} 
}

the environment is : RC5

2
  • What is "user type" and where does it come from? Do you want the user type to be reflected in in the browsers URL bar? You can just use <div *ngIf="userType=='x'">x</div><div *ngIf="userType=='y'">y</div>` Commented Aug 24, 2016 at 9:21
  • thank you, like if(u.type=='manager') navigate('edit'); else navigate('view'); Commented Aug 24, 2016 at 9:33

3 Answers 3

0

Something like this:

 if (this.user.userType === UserType.x1) {
  this.router.navigate(['/edit']); } 
else if (this.user.userType === UserType.x2) {
            this.router.navigate(['/view']);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Inject the router and call navigate(...)

@NgModule({
  imports: [ SharedModule ,RouterModule.forChild([
    { path: 'edit', component: EditComponent },
    { path: 'view', component: ViewComponent },
  ])],
  declarations: [
    EditComponent, ViewComponent
  ],
})
export default class MainModule { 
  constructor(private u:UserService, router:Router, ){
    if(u.type=='manager') {
      router.navigate(['/edit']);
    } else {
      router.navigate(['/view']);
    }
  } 
}

See also Angular 2 router.navigate

2 Comments

Unhandled Promise rejection: Bootstrap at least one component before injecting Router.
How does your AppModule look like
0

after testing: when in lazy load mode, writting this code in ngModule can cause an infinite loop

constructor(private router:Router){
     console.log("MainModule 39");
     this.router.navigateByUrl("/tabs-demo/tab3");
}

when not in lazy load mode, writting that code in ngModule can cause

Unhandled Promise rejection: Bootstrap at least one component before injecting Router

the answer is: writting that navigation code in the first component.

testing in RC5

thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.