bootstrap 型号中的两个 Angular 路由器插座

Two Angular router-oulets in bootstrap modal

我在我的 Angular 应用程序中添加了其他模式,两者都有不同

<router-outlet></router-outlet>

前一个工作正常,但在添加具有不同路线的第二个模式后,第一个停止工作。我在路由器插座上尝试了不同的 name 属性,但似乎没有任何效果。 如果我注释掉另一个,一次只有一个在工作。

<button type="button" data-target="#settings2" data-toggle="modal" routerLink="setup_season">                         
   Season
</button>

<button type="button" data-target="#settings" data-toggle="modal" routerLink="setup_package">                         
  Package
</button>

第一个模态

 <div id="settings" class="modal fade" role="dialog">
      <div class="modal-dialog">

         <!-- Modal -->
      <div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog">

          <!-- Modal content-->
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal">&times;</button>
              <h4 class="modal-title">Modal Header</h4>
            </div>
            <div class="modal-body">
                <router-outlet></router-outlet> // already tried with name attribute
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
          </div>

        </div>
      </div>

第二模态

<div id="settings2" class="modal fade" role="dialog">
  <div class="modal-dialog">

     <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Modal Header</h4>
        </div>
        <div class="modal-body">
            <router-outlet></router-outlet> // already tried with name attribute
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>

    </div>
  </div>

你需要使用辅助路由,我把我自己代码的类似案例贴出来,你的代码也试试:

  <div class="side-panel">
      <!-- the auxiliary outlet -->
      <router-outlet name="sidePanel"></router-outlet>
  </div>

  <div class="main-content m-0 p-0">
      <!-- the primary outlet -->
      <router-outlet></router-outlet>
 </div>

路由配置应该与此类似:

const routes: Routes = [
  {
    path: '',
    redirectTo: '/app/auth/login',
    pathMatch: 'full'
  },
  {
    path: 'app',
    children: [
       {
         path: 'main',
         component: MainComponent
       }, 
       {
         path: 'side-menu',
         component: SlidePanelComponent,
         outlet: 'sidePanel',  // You need to mention the name of the auxiliary route
       }
     ]
]

最后,你需要在routerLink中提到插座的名称,就像这样:

  <a [routerLink]="[{ outlets: { sidePanel: 'side-menu'}}]">