typescript - Angular 2 Route在嵌套组件中发生变化

标签 typescript angular angular2-routing

我有一个相当简单的 Angular 2 问题。我有一个主视图,顶部有一个工具栏,其正下方有一个路由器 socket 。

ma​​inView.ts

@Component({
    selector: 'pod-app',
    template: `
        <pod-toolbar></pod-toolbar>

        <router-outlet></router-outlet> <!-- USE THIS router-outlet -->
    `,
    directives: [ ToolbarComponent, RouteComponent, ROUTER_DIRECTIVES ],
    providers: [
        EventService,
        ROUTER_PROVIDERS
    ]
})

@RouteConfig([
    {
        path: '/list',
        name: 'ListView',
        component: ListViewComponent,
        useAsDefault: true
    },
    {
        path: '/tileView',
        name: 'TileView',
        component: TileViewComponent
    },
    {
        path: '/pdfView',
        name: "PdfView",
        component: PdfViewComponent
    }
])

export class MainComponent implements OnInit { 
    ...blah...blah...blah
 }

然后我有我的 toolbar.ts 嵌套在里面是我的路线导航列表

toolbar.ts

@Component({
    selector: 'pod-toolbar',
    template: `
        <div class="demo-toolbar">
            <p>
                <md-toolbar color="primary">

                    <pod-routes></pod-routes> <!-- appRoutes.ts component -->

                </md-toolbar>
            </p>

        </div>
    `,
    directives: [MdToolbar, MdButton,RouteComponent, ROUTER_DIRECTIVES],
    providers: [ROUTER_PROVIDERS]
})

export class ToolbarComponent implements OnInit {
    ...blah...blah...blah
}

然后这是嵌套在工具栏内的路由链接,需要在 mainView 中更改工具栏下方的 View

appRoutes.ts

@Component({
    selector: 'pod-routes',
    template: 
    `
        <nav>
            <a [routerLink]="['/ListView']"><img src="pathto.png" height="45" width="45"></a>
            <a [routerLink]="['/TileView']"><img src="pathto.png" height="40" width="45" ></a>
            <a [routerLink]="['/PdfView']"><img src="pathto.png" height="40" width="45" ></a>
        </nav>
    `,
    directives: [ROUTER_DIRECTIVES]
})

export class RouteComponent{

}

那么,我做错了什么?这是我希望它设置的方式,但如果这不可行,那么也许我可以将路由移动到工具栏中,如果那不可行,那么我可以将整个工具栏移动到 mainView 中,我不这样做我想做,但如果必须,我会做。

更新

从 appRoutes.ts 中删除了 ROUTER_PROVIDERS,仍然有同样的问题。

更新 2

在回答建议的 url 路由中添加了 '/',同样的问题,地址栏中的 url 发生了变化,但工具栏下方的 ui 没有变化

更新 3

从 toolbar.ts 和 mainView.ts 文件中删除了 providers: [ROUTER_PROVIDERS] 并在 app.ts 中引导了 ROUTER_PROVIDERS

bootstrap(MainComponent, [ROUTER_PROVIDERS, provide(LocationStrategy, {useClass: HashLocationStrategy}) ] )
     .catch(err => console.error(err));

似乎已经成功了。非常感谢 Gunter 支持我并帮助我解决问题。他的 Plunker 真的帮了大忙!

最佳答案

更新2

Plunker example

更新

在路由名称前加上/ 来告诉根路由器应该导航到这个路由:

<a [routerLink]="['/ListView']"><img src="pathto.png" height="45" width="45"></a>
<a [routerLink]="['/TileView']"><img src="pathto.png" height="40" width="45" ></a>
<a [routerLink]="['/PdfView']"><img src="pathto.png" height="40" width="45" ></a>

原创

不要多次提供ROUTER_PROVIDERS

providers: [
    EventService,
    ROUTER_PROVIDERS
]

ROUTER_PROVIDERS 应该只在根组件中提供一次,而不是在其他任何地方(或者在 boostrap(...) 中提供)

关于typescript - Angular 2 Route在嵌套组件中发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36844717/

相关文章:

typescript - NestJs Swagger 混合类型

typescript - 为什么 vue 在第一次编译时无法识别 TS 语法,但在第二次编译时可以正常工作?

angular - ionic 4 - 如何检索传递给模态的数据

angular - 无法绑定(bind)到 'routerLink',因为它不是已知属性

angular - 从头开始使用 Angular 2 的旋转木马/ slider

node.js - 续写 TypeScript : How to use `createdAt` in scope's `where` condition if it is not as part of model's attributes?

javascript - 类型错误 : Cannot read property '0' of undefined in angular2 for using index in datepicker

html - 带有 Twitter Bootstrap 的 Angular 2

Angular 路由不起作用。抛出错误的组件不是模块的一部分

angular - "A componentless route cannot have a named outlet set"