【问题标题】:Angular 2 Routing Not Finding RoutesAngular 2路由找不到路由
【发布时间】:2016-10-27 23:39:09
【问题描述】:

我无法理解当前的问题,即单击 routerlink 时无法加载新页面。

我已经为链接尝试了许多不同的结构,但无论我将其更改为控制台中的错误提示

VM39436:48 例外:未捕获(承诺中):错误:无法匹配任何 路线。 URL 段:“流”

我使用模板布局,然后使我的页面成为这些布局的子级。一种布局是安全的,一种布局是公开的。

在 app.routing.ts 中

const APP_ROUTES: Routes = [
      {
        path: '',
        redirectTo: 'stream',
        pathMatch: 'full',
    },
    {
        path: 'profile',
        component: SecureLayoutComponent,
        data: {
            title: 'Secure Views'
        },
        children: [
            {
                path: 'Profile',
                loadChildren: 'profile/profile.module#ProfileModule'
            }
        ]
    },
    {
        path: '',
        component: PublicLayoutComponent,
        data: {
            title: 'Public Views'
        },
        children: [
            {
                path: 'stream',
                loadChildren: 'stream/stream.module#StreamModule',
            }
        ]
    }
];

现在我有一个用于配置文件的文件夹和一个用于流的文件夹。

所以当你移动到流目录时,这是 stream-routing.module.ts

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

import { StreamComponent }   from './stream.component';

const routes: Routes = [
    {
        path: '',
        component: StreamComponent,
        data: {
            title: 'Stream'
        }
    }
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule]
})
export class StreamRoutingModule {}

这是profile-routing.module.ts

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

import { ProfileComponent }   from './profile.component';

export const routes: Routes = [
    {
        path: '',
        data: {
            title: 'Secure Pages'
        },
        children: [
            {
                path: 'profile',
                component: ProfileComponent,
                data: {
                    title: 'Profile Page'
                }
            }
        ]
    }
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule]
})
export class ProfileRoutingModule {}

因此,当应用程序呈现时,它可以正常加载。但是当我尝试点击时,

  <a class="nav-link"  routerLinkActive="active" [routerLink]="['../profile/profile']">Profile</a>

或者

  <a class="nav-link"  routerLinkActive="active" [routerLink]="['../profile']">Profile</a>

  <a class="nav-link"  routerLinkActive="active" [routerLink]="['/profile']">Profile</a>

我得到了上面提到的错误,

所以从/目录

/app.routing.ts

/stream/stream.component.ts & stream-routing.module.ts

/profile/profile.component.ts & profile-routing.module.ts // 不能从公共布局访问这个。

【问题讨论】:

  • 我没有看到任何带有path: 'stream' 的路线
  • 查看 app.routing 和 stream-routing.module 它工作正常。这是我无法上班的个人资料溃败。但是,当流作为主页加载并且我单击链接转到配置文件路由时,错误提到了流。这真的让我很困惑。从昨天开始我就一直坚持这个问题,这就是我发帖寻求帮助的原因。我对这个很迷茫。
  • 您有 2 条路径,路径为 ''(第一个重定向路径和 PublicLayoutComponent)。目的是什么?

标签: angular typescript mean-stack


【解决方案1】:

我认为你只需要删除重定向路由

  {
    path: '',
    redirectTo: 'stream',
    pathMatch: 'full',
  },

喜欢

const APP_ROUTES: Routes = [
    {
        path: 'profile',
        component: SecureLayoutComponent,
        data: {
            title: 'Secure Views'
        },
        children: [
            {
                path: 'Profile',
                loadChildren: 'profile/profile.module#ProfileModule'
            }
        ]
    },
    {
        path: '',
        component: PublicLayoutComponent,
        data: {
            title: 'Public Views'
        },
        children: [
            {
                path: 'stream',
                loadChildren: 'stream/stream.module#StreamModule',
            }
        ]
    }
];

【讨论】:

  • 当我在页面加载 localhost:3000 时删除该路由重定向时,页面中不会加载任何内容。 VM44183:48 异常:未捕获(承诺):错误:无法匹配任何路由。网址段:''
  • 您在&lt;head&gt; 中有&lt;base href="/"&gt;?你能在 Plunker 中复制吗?
  • 我在 中有 哦,是的,我可以尝试真正快速地做到这一点。谢谢
  • Plunker 提供了一个现成的模板。只需使用编辑器中的 [new|v] 按钮并选择 Angular > 2.0 TS 并添加您的代码。双击菜单中的文件允许重命名。要将文件放入文件夹,只需在名称前加上文件夹名称 (src/my.component.ts)
【解决方案2】:

我添加此答案是为了帮助在多级路由、子路由或身份验证保护方面遇到问题的任何其他人。

我在我的应用程序中使用了两个模板,

/templates/public.component.ts
/templates/secure.component.ts

这里是secure-layout.component.ts 文件

import { Component, OnInit }        from '@angular/core';
import { Router }                   from '@angular/router';
import { Auth }                     from './../services/auth.service';

@Component({
    providers: [ Auth ],
    selector: 'app-dashboard',
    templateUrl: './secure-layout.component.html'
})
export class SecureLayoutComponent implements OnInit {

    constructor( private router: Router, private auth: Auth ) { }

    ngOnInit(): void { }
}

这是 public-layout.component.ts 文件

import { Component, OnInit }    from '@angular/core';
import { Router }               from '@angular/router';
import { Auth }                 from './../services/auth.service';

@Component({
    providers: [ Auth ],
    selector: 'app-dashboard',
    templateUrl: './public-layout.component.html'
})
export class PublicLayoutComponent implements OnInit {

    constructor( private router: Router, private auth: Auth ) { }

    ngOnInit(): void { }
}

这样我可以为安全模板添加保护,将任何未经授权的流量重定向回公共模板。所以从 /app.routing.ts 文件我为布局创建路由,然后在我创建的组件内部创建子路由,这些子路由成为相应模板的子路由。

app.routing.ts

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

import {Guard}                      from "./services/guard.service";

//Layouts
import { PublicLayoutComponent }    from './layouts/public-layout.component';
import { SecureLayoutComponent }    from './layouts/secure-layout.component';

import { STREAM_ROUTES }            from "./stream/stream.routes";
import { PROFILE_ROUTES }           from "./profile/profile.routes";

const APP_ROUTES: Routes = [
    { path: '', redirectTo: '/stream', pathMatch: 'full', },
    { path: '', component: PublicLayoutComponent, data: { title: 'Public Views' }, children: STREAM_ROUTES },
    { path: '', component: SecureLayoutComponent, canActivate: [Guard], data: { title: 'Secure Views' }, children: PROFILE_ROUTES }
];



export const routing = RouterModule.forRoot(APP_ROUTES);

/stream/stream.routes.ts

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

@Component({
  templateUrl: './stream.component.html',
})

export class StreamComponent {

  constructor() { }

}

/profile/profile.routes.ts

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

import { ProfileComponent }   from './profile.component';

export const PROFILE_ROUTES: Routes = [
    { path: '', redirectTo: 'profile', pathMatch: 'full' },
    { path: 'profile', component: ProfileComponent }
];

在此示例中,流是我保留公开视图的位置,而个人资料是我所有安全视图的位置。因此,如果我想创建一个新的公共视图,我会进入流并创建一个新路由,然后创建 html 文件以及 component.ts 文件。

我希望这会有所帮助。我认为这是处理任何应用程序路由的一种非常好的方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    相关资源
    最近更新 更多