【问题标题】:Multiple Router outlets in different templates Angular4不同模板Angular4中的多个路由器出口
【发布时间】:2018-01-19 13:27:48
【问题描述】:

我使用主 app.component.html 中的 router-outlet 标记登录,如果登录成功,我会导航到主页,这是一个不同的组件,由工具栏、侧导航和一个显示我的仪表板内容的区域组成。现在在我的主页中,我需要另一个路由器插座,它会在侧导航中选择选项时显示内容

我的项目结构是:

src

--login
----login.component.html
----login.component.ts
----login.component.css
--home-page
    dashboard
    --dashboard.component.html
    --dashboard.component.ts
----home.component.html
----home.component.ts
----home.component.css
--application-details
     app-overview
     --app-overview.component.html
     --app-overview.component.ts
----application.component.html
----application.component.ts
--users
----users.component.html
----users.component.ts

在我的app.component.html 中有以下内容

<router-outlet></router-outlet>

login.componnet.ts

成功后this.route.navigate(['/home-page'])

在我的app-routing.module.ts

{ path:'', redirectTo:'/login',pathMatch:'full'},
{ path: 'login', component: LoginComponent},
{ path:'/home-page',component:HomePgaeComponent,
    children:[
         { path:'',component:DashboardComponent,outlet:'dashboard'},
         { path:'user',component:UsersComponent,outlet:'user'}, ]
   },

现在只要我根据成功条件登录就会触发页面 导航到这样的主页内的仪表板

    <tool-bar></tool-bar>    
    <div>
    <side-menu></side-menu> 
    <div class="main-content">

       <router-outlet name="dashboard"></router-outlet>
       <router-outlet name="user"></router-outlet>

    </div>
  </div> 

在这里,当我从侧导航单击用户选项时,它会附加仪表板 内容以及“用户”内容。我还有其他选项 第一次完美加载着陆页但是当我选择其他 side-nav options 所有其他内容都混在一起了。所以当我选择选项时 从侧面导航,相应的内容应加载到的内容区域 主页。另一种情况是当我导航到主页时,我需要导航 当我在仪表板组件中执行一些单击操作时到另一个页面。 我已经阅读了教程,他们在那里传递了一些 id 服务,我不知道该怎么做。据我所知,我知道 将出口名称传递给路由器出口将获取该内容 组件同样我提到的许多路由器 生成。

【问题讨论】:

    标签: javascript html angular angular-routing


    【解决方案1】:

    在这里,当我从侧导航单击用户选项时,它会附加 仪表板内容以及“用户”内容

     <router-outlet name="dashboard"></router-outlet>
     <router-outlet name="user"></router-outlet>
    

    两者都是同一个组件的子组件

    { path:'/home-page',component:HomePgaeComponent,
        children:[
             { path:'',component:DashboardComponent,outlet:'dashboard'},
             { path:'user',component:UsersComponent,outlet:'user'}, ]
    },
    

    要么您使用相同的路由器插座来呈现仪表板和用户,要么您在仪表板内有用户路由器插座并且用户是仪表板的子级。

    【讨论】:

    • 我不想同时渲染两个内容,我想渲染单个内容,例如当我从 sidenav 单击用户选项时,我想加载用户内容来代替仪表板内容。此外,如果我从 sidenav 中选择用户选项,只要我登录它就会立即登陆仪表板内容。
    猜你喜欢
    • 2017-12-07
    • 2019-08-11
    • 2017-03-23
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 2016-12-09
    相关资源
    最近更新 更多