简体   繁体   English

以角度4加载路由器出口外的页面

[英]load a page outside the router-outlet in angular 4

I am working on a angular 4 application and I want load login.page.ts outside of the router-outlet 我正在开发一个有角度的4应用程序,我想在router-outlet外加载login.page.ts

this is my home.component.html file 这是我的home.component.html文件

<div class="container">
   <top-nav-bar></top-nav-bar>
   <lett-nav-bar></lett-nav-bar>
   <router-outlet></router-outlet>
</div>

routes configs 路线配置

const MAINMENU_ROUTES: Routes = [
    { path: 'login', component: LoginComponent },
    { path: '', redirectTo: 'home', pathMatch: 'full'},
    { path: 'home', component: HomeComponent, canActivate: [AuthGuard]}
];

with this, I can load login page inside the router but I want to load login page in full screen before coming to the router-outlet. 有了这个,我可以在路由器内加载登录页面,但我想在进入路由器插座之前全屏加载登录页面。

You cannot route to a page without a router outlet. 没有路由器插座,您无法路由到页面。 Based on what you are describing, it sounds like you need another router outlet at a higher level, say in an App Component. 根据您所描述的内容,听起来您需要更高级别的其他路由器插座,例如在App组件中。 Something like this: 像这样的东西:

<div class="container">
   <router-outlet></router-outlet>
</div>

Then you can route the login page and the home component into this router outlet. 然后,您可以将登录页面和主组件路由到此路由器插座。

Your route configuration would then look something like this: 您的路线配置将如下所示:

    { path: 'login', component: LoginComponent },
    { path: 'home', component: HomeComponent, canActivate: [AuthGuard], 
      children: [
       { path: 'child1', component: Child1Component },
       { path: 'child2', component: Child2Component }
      ]
    }

The login and home routes would then appear in the App Component's router outlet, so the login page would be shown full screen with no nav bars. 登录页面和主页路由将出现在App Component的路由器插座中,因此登录页面将全屏显示,没有导航栏。 And the child1 and child2 routes would appear in the Home Component's router outlet. child1和child2路由将出现在Home Component的路由器插座中。

DeborahK answer is the recommended solution, but there is a way to avoid having another router outlet by using a query parameter that is recognized in App Component which can react and have some elements (header, footer) disappear. DeborahK答案是推荐的解决方案,但是有一种方法可以避免使用App Component中识别的查询参数来获得另一个路由器插座,该参数可以做出反应并使某些元素(页眉,页脚)消失。

app.component.html app.component.html

<app-nav-menu [hidden]="hasFullView"></app-nav-menu>
<div class="{{!hasFullView ? 'body-container' : ''}}">
  <router-outlet></router-outlet>
</div>
<app-footer [hidden]="hasFullView"></app-footer>

app.component.ts app.component.ts

hasFullView = false;

private setHasFullView() {
    this.activatedRoute.queryParams.subscribe(params => {
        this.hasFullView = params["hasFullView"] || false;
    });
}

ngOnInit() {
  this.setHasFullView();
}

usage example 用法示例

showPrintView() {
  const url = `module/user-edit-printout/${userId}?hasFullView=true`;
  window.open(url);
}

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

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