简体   繁体   English

组件模板中的Angular 2 router-outlet

[英]Angular 2 router-outlet within component template

I have been playing around with routes in Angular 2 but I have run into an issue that I cannot find an answer for. 我一直在玩Angular 2中的路线,但我遇到了一个我无法找到答案的问题。 My routes are "working" but not the way I am hoping for. 我的路线“工作”,但不是我希望的方式。 I am trying to use this like you would use ng-view from angular 1, my header and footer never change, and when my url changes new "stuff" is placed in my content. 我试图使用这个,就像你使用角度1的ng-view,我的页眉和页脚永远不会改变,当我的网址改变时,新的“东西”放在我的内容中。

Here is what I have now which "works" 这就是我现在所拥有的“工作”

import { Component } from '@angular/core';
import { HeaderComponent } from './shared/header/header.component';
import { ContentComponent } from './shared/content/content.component';
import { FooterComponent } from './shared/footer/footer.component';
import { TestComponent } from './components/test.component';
import { Routes, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from '@angular/router';

@Routes([
    {path: '/test', component: TestComponent }
])
@Component({
    selector: 'app',
    template: `<header></header>
    <a [routerLink]="['/test']">Click for testComponent</a>
    <router-outlet></router-outlet>
    <footer></footer>`,
    directives: [HeaderComponent, ContentComponent, FooterComponent, ROUTER_DIRECTIVES]
})
export class AppComponent { }

What I am trying to do is put the router outlet inside of my ContentComponent so that essentially the router just dumps the new stuff directly into the main area of my app. 我想要做的是将路由器插座放在我的ContentComponent内部,这样路由器就可以将新的东西直接转储到我的应用程序的主要区域。 When I try to rewrite: 当我尝试重写时:
<router-outlet></router-outlet>
as
<content><router-outlet></router-outlet></content>
the routing stops working, no error message, just stops. 路由停止工作,没有错误消息,只是停止。 I also do not see my testComponent in the DOM when I inspect it with the devtools. 当我用devtools检查它时,我也没有在DOM中看到我的testComponent。 So I thought, well I will put the router-outlet inside the template of my ContentComponent like so: 所以我想,我会将路由器插座放在我的ContentComponent的模板中,如下所示:

import { Component } from '@angular/core';

@Component({
    selector: 'content',
    template: `<router-outlet></router-outlet>`
})
export class ContentComponent { }

However when I do this I get the error message: Cannot find default outlet. 但是,当我这样做时,我收到错误消息:找不到默认插座。

I am missing something but the lack of documentation makes it very difficult to figure out, I would think there would be a way to define within @Routes where my outlet is but again, can't find any documentation that isn't for the deprecated-router. 我遗漏了一些东西,但缺乏文档使得很难弄明白,我认为有一种方法可以在@Routes中定义我的插座,但是再次找不到任何不被弃用的文档-路由器。 Hope someone can shed some light on this. 希望有人可以对此有所了解。 Thanks in advance. 提前致谢。

Note: general code critiques would also be helpful, I am very new to Angular 2 and would love the advice. 注意:一般代码批评也会有所帮助,我对Angular 2很新,并且很喜欢这个建议。

The way you call ContentComponent via <content> tag makes ContentComponent a child of your AppComponent and you can't place a router-outlet inside a child component's template. 通过<content>标记调用ContentComponent的方式使ContentComponent成为AppComponent的子项,并且您不能将路由器插座放在子组件的模板中。 One way to achieve what you are after is to create a default route for the ContentComponent in the AppComponent: 实现目标的一种方法是在AppComponent中为ContentComponent创建默认路由:

@Routes([
    { path: '/', component: ContentComponent }
    { path: '/content', component: ContentComponent }
])

And AppComponent's template will be: 而AppComponent的模板将是:

<header></header>     
<router-outlet></router-outlet>
<footer></footer>

Now you can define nested routes in your ContentComponent like this: 现在,您可以在ContentComponent中定义嵌套路由,如下所示:

@Routes([
    { path: 'content1', component: Content1Component }
])
@Component({
    selector: 'content',
    template: `<router-outlet></router-outlet>`
})
export class ContentComponent { }

And if you have a menu in your header component and want to link to each nested content you can do this: 如果您的标题组件中有一个菜单并想要链接到每个嵌套内容,您可以这样做:

<a [routerLink]="['/content', 'content1']">Content1</a>

Which will load Content1Component into the ContentComponent's <router-outlet> 这会将Content1Component加载到ContentComponent的<router-outlet>

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

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