简体   繁体   English

Angular 2路由器辅助路由不在redirectTo上工作

[英]Angular 2 router auxiliary routes not working on redirectTo

I recently started a new project based on angular 2.0.0 in combination with Angular Router (V3.0.0). 我最近开始了一个基于角度2.0.0和Angular Router(V3.0.0)的新项目。 Thereby I have issues with the auxiliary routes. 因此,我有辅助路线的问题。

I want to split up my application in different views that appear in every state like navigation bar, main content, footer etc. So that when the user interact with the site only the part of the page that needs to be changed (fe the content) is changed and all other views are left as they are. 我想在不同的视图中分割我的应用程序,这些视图出现在导航栏,主要内容,页脚等各个状态中。这样当用户与站点交互时,只需要更改页面的一部分(内容)已更改,所有其他视图保持不变。 Therefore I wanted to use the routers auxiliary feature as discussed in this talk: 因此,我想使用本讲座中讨论的路由器辅助功能:

Angular Router Talk @AC2015 Angular Router Talk @ AC2015

I set up my files as follows: 我按如下方式设置文件:

app.html app.html

<router-outlet name="navigation"></router-outlet>

<main>
    <router-outlet></router-outlet>
</main>

<router-outlet name="footer"></router-outlet>

app.routes.ts app.routes.ts

import {Routes} from '@angular/router';

export const rootRouterConfig: Routes = [
  {path: '', redirectTo: 'start(navigation:/navigation)', pathMatch: 'full'}
];

start.routes.ts start.routes.ts

import {Routes} from '@angular/router';
import {StartContentComponent} from './startContent/startContent.component';
import {StartNavigationComponent} from "./startNavigation/startNavigation.component";  

export const startRouterConfig: Routes = [
    {path: 'start', component:  StartContentComponent},
    {path: 'navigation', component: StartNavigationComponent, outlet: 'navigation'}
];

Now the problem is: If I manually enter 现在的问题是:如果我手动输入

http://localhost:3000/#/start(navigation:/navigation)

into the url of the browser both components are displayed correctly. 进入浏览器的url,两个组件都正确显示。 But the redirect in app.routes.ts is not working and I get the following error when accessing http://localhost:3000/#/ : 但app.routes.ts中的重定向无效,访问http:// localhost:3000 /#/时出现以下错误:

Uncaught (in promise): Error: Cannot match any routes: '' 未捕获(承诺):错误:无法匹配任何路线:''

The official Angular 2 docs say nothing about auxiliary routing yet only that it will be discussed in the future. 官方Angular 2文档没有提及辅助路由,只是将来会讨论它。 Apart from that I found some other blogposts but none of them discussed auxiliary routes in combination with redirects. 除此之外,我还发现了其他一些博文,但没有一个与重定向相结合地讨论辅助路线。

Does anyone know what my problem is or have similar issues? 有谁知道我的问题是什么或有类似的问题?

Is there another approach to structure my page to achieve what I want? 是否有另一种方法来构建我的页面以实现我想要的?

Should I maybe switch to Ui-router? 我应该切换到Ui路由器吗?

I had the same problem, and Mr. Binary's solution seemed to be on the right track, but the 'Componentless Routes' part of this site got me there. 我遇到了同样的问题,Binary先生的解决方案似乎在正确的轨道上,但是这个网站的“无组件路线”部分让我在那里。

My solution: 我的解决方案

export const RedirectionIncludingAuxRoutes: RouterConfig = [
    {
        path: 'redirect-me',
        redirectTo: 'redirected-with-aux'
    },
    {
        path: 'redirected-with-aux',
        children: [
            {
                path: '',
                component: PrimaryComponent
            },
            {
                path: '',
                component: SecondaryComponent,
                outlet: 'auxiliary'
            }
        ]
    }
]

That's recommended approach when you have more changing parts. 当你有更多零件时,这是推荐的方法。
Anyway try changing 无论如何尝试改变

redirectTo: 'start(navigation:/navigation)'
to
redirectTo: 'start/(navigation:navigation)'

And

{path: 'start', component:  StartContentComponent},
{path: 'navigation', component: StartNavigationComponent, outlet: 'navigation'}
to
{path: 'start', component:  StartContentComponent,
   children: [
      {path: 'navigation', component: StartNavigationComponent, outlet: 'navigation'}
]},

Also i would recommend this article to router: 我还会向路由器推荐这篇文章:
http://blog.angular-university.io/angular2-router/ http://blog.angular-university.io/angular2-router/

You are almost there. 你快到了。 Just one step away. 只有一步之遥。 You need to wire rootRouterConfig in one of the following ways 您需要通过以下方式之一连接rootRouterConfig

import {Routes , RouterModule } from '@angular/router';

@NgModule({
  imports: [RouterModule.forRoot(rootRouterConfig)],
  exports: [RouterModule],
})

OR 要么

import {ModuleWithProviders } from '@angular/core';
import {Routes , RouterModule } from '@angular/router';

export const routing: ModuleWithProviders =RouterModule.forRoot(rootRouterConfig);

Let me know which one works for you. 让我知道哪一个适合你。

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

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