我无法导航到另一个 router-outlet 内部。

huangapple go评论59阅读模式
英文:

i can't navigate to router-outlet inside another

问题

以下是您要翻译的内容:

我在我的项目中有两个 router-outlet,并且一个嵌套在另一个内部,第一个是默认的应用 router-outlet,第二个位于 'home' 组件中,当我点击工具栏时,我想在其中显示其他组件,这是我找到的导航到第二个的解决方案<br>
HTML 文件:

   &lt;mat-toolbar &gt;
      &lt;a routerLink=&quot;/sessionag&quot;&gt;Session&lt;/a&gt;
      
    
      &lt;button mat-button style=&quot;background-color: #80D0D0&quot; (click)=&quot;logout()&quot;&gt;logout&lt;/button&gt;
    
    &lt;/mat-toolbar&gt;
    &lt;mat-drawer-container &gt;
      &lt;mat-drawer-content style=&quot;color: #333333 ;background-color: white&quot;&gt;
        &lt;router-outlet name=&quot;second&quot;&gt;&lt;/router-outlet&gt;
    
      &lt;/mat-drawer-content&gt;
    &lt;/mat-drawer-container&gt;

AppRoutingModule 文件:

 const routes: Routes = [
 
  {path:&#39;sessionag&#39;,
    component:SessionAgComponent,outlet:&#39;second&#39;},
  
];

但这对我来说不起作用,我不知道为什么。<br>
我做错了什么,还是它不像这样工作?

英文:

I have 2 router-outlet in my project and one inside on the other the first one is the default app router-outlet and the second one is inside one of the 'home' component in which I want to show other components when I click on the toolbarso this was the solution that I found to navigate to the second one <br>
html file :

   &lt;mat-toolbar &gt;
      &lt;a routerLink=&quot;/sessionag&quot;&gt;Session&lt;/a&gt;
      
    
      &lt;button mat-button style=&quot;background-color: #80D0D0&quot; (click)=&quot;logout()&quot;&gt;logout&lt;/button&gt;
    
    &lt;/mat-toolbar&gt;
    &lt;mat-drawer-container &gt;
      &lt;mat-drawer-content style=&quot;color: #333333 ;background-color: white&quot;&gt;
        &lt;router-outlet name=&quot;second&quot;&gt;&lt;/router-outlet&gt;
    
      &lt;/mat-drawer-content&gt;
    &lt;/mat-drawer-container&gt;

AppRoutingModule file :

 const routes: Routes = [
 
  {path:&#39;sessionag&#39;,
    component:SessionAgComponent,outlet:&#39;second&#39;},
  
];

but this is not working for me i don't know why .<br>
did i do something wrang or it does not work like this

答案1

得分: 2

If you're not navigating to the primary router, you have to tell angular what router-outlet you want to route to.

Using the routerLink directive you can specify the outlet you want to route to:

&lt;a 
  [routerLink]=&quot;[{ outlets: {second: [&#39;sessionag&#39;] } }]&quot;&gt;
  Session
&lt;/a&gt;

Demo on Stackblitz

英文:

If you're not navigating to the primary router, you have to tell angular what router-outlet you want to route to.

Using the routerLink directive you can specify the outlet you want to route to:

&lt;a 
  [routerLink]=&quot;[{ outlets: {second: [&#39;sessionag&#39;] } }]&quot;&gt;
  Session
&lt;/a&gt;

Demo on Stackblitz

答案2

得分: 1

在app-routing-module中,您需要将组件添加为另一个组件的子组件,以在第二个router-outlet中使用。具体如下所示:

// 在app-routing-module中
const routes: Routes = [
  {
    path: 'home',
    component: HomepageComponent,
    children: [
      {
        path: 'sessionag',
        component: SessionAgComponent
      },
      {
        path: 'sessiontek',
        component: SessionTekComponent
      }
    ]
  }
];

在.ts文件中,您可以使用以下代码导航到所需的路由:

this.router.navigate(['/home/sessionag']);

在HTML文件中,您可以像这样使用router-outlet:

<router-outlet></router-outlet>

希望这有所帮助。

英文:

so what I was looking for actually is a router outlet inside another and the answer that I found is to add the component as children of the component in which I want to add the second router-outlet like this :<br>
in app-routing-module

    const routes: Routes = [
      {path:&#39;home&#39;,
    component:HomepageComponent,
    children:[
      {path:&#39;sessionag&#39;,
        component:SessionAgComponent},
      {path:&#39;sessiontek&#39;,
        component:SessionTekComponent},
    ]
  },
];

in the .ts file

this.router.navigate([&#39;/home/sessionag&#39;]);

in HTML file

 &lt;router-outlet &gt;&lt;/router-outlet&gt;

huangapple
  • 本文由 发表于 2020年1月6日 22:32:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613876.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定