简体   繁体   English

单击按钮导航到不同模块上的组件

[英]Navigate to a component on different module on button click

I have 2 different modules, one is common UI module in which I have sso-Button Component, and other one is SSO Module, in which I have initiatesso Component.我有 2 个不同的模块,一个是通用 UI 模块,其中我有 sso-Button 组件,另一个是 SSO 模块,其中我有initialsso 组件。

There is a button in sso-button component, if click that button, it should redirect to initiatesso component. sso-button 组件中有一个按钮,如果单击该按钮,它应该重定向到initialsso 组件。

I have tried following code.我试过以下代码。

Module: CommonUI Module Sso-button.component.html:模块:CommonUI 模块 Sso-button.component.html:

<button class="logo-button" (click)="alert()" style="position:absolute;"> 
</button>

Sso-button.component.ts: Sso-button.component.ts:

  public alert() {
    alert("");
    this.router.navigateByUrl(`initiatesso`);    
   }

Module: SSO Module模块:单点登录模块

sso.module.ts: sso.module.ts:

export const ssoRoutes: Routes = [

  {
    path: 'initiatesso',
    component: InitiatessoComponent
  }
];

@NgModule({
  imports: [    
    RouterModule.forChild(ssoRoutes),    
  ],
  declarations: [InitiatessoComponent],
  exports: [InitiatessoComponent]
})
export class SsoModule { }

initiatesso.component.html启动so.component.html

initiatesso works!

initiatesso.component.ts启动so.component.ts

export class InitiatessoComponent implements OnInit {
  constructor() { }
  ngOnInit() {
  }
}

But on the button click I am not able to redirect to the initiatesso page, not sure where I am going wrong.但是在单击按钮时,我无法重定向到启动页面,不确定我哪里出错了。 Please help.请帮忙。

For UI module you need to import your SsoModule对于 UI 模块,您需要导入 SsoModule

So your code would look like this所以你的代码看起来像这样

    @NgModule({
      bootstrap: [AppComponent],
      imports: [
        SsoModule,
        RouterModule.forRoot(mainRoutes)
      ]

Then in your main routing you need to add your sso routing然后在您的主路由中,您需要添加您的 sso 路由

export const mainRoutes: Routes = [

  {
    path: 'initiatesso',
    component: InitiatessoComponent
  }
]; 

And change your SSOModule code into this并将您的 SSOModule 代码更改为此

export const ssoRoutes: Routes = [

  {
    path: '',
    component: InitiatessoComponent
  }
];

@NgModule({
  imports: [    
    RouterModule.forChild(ssoRoutes),    
  ],
  declarations: [InitiatessoComponent],
  exports: [InitiatessoComponent]
})
export class SsoModule { }

RouterModule.forChild is use when you have sub route for the module like so you only need to define sub route in Route module for your SSO module like RouterModule.forChild 当你有模块的子路由时使用,所以你只需要在路由模块中为你的 SSO 模块定义子路由,比如

export const ssoRoutes: Routes = [

  {
    path: '',
    component: InitiatessoComponent
  },
  {
    path: 'abc',
    component: InitiatessoABCComponent
  }
];

module/abc/模块/ABC/

So now you can use所以现在你可以使用

this.router.navigateByUrl(`initiatesso`);   

Please let me know if you need any help如果您需要任何帮助,请告诉我

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

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