【问题标题】:Angular 2 nested routes throwing errorsAngular 2嵌套路由抛出错误
【发布时间】:2016-10-25 22:03:46
【问题描述】:

我正在使用 ASP.NET Core 中的 @angular 开发 Angular 2 应用程序。我有一个位于基本 url 的登录页面,它有自己的布局文件、索引页面和控制器。

我想添加的是我网站 /toolkit 的整个部分。首先,我使用我的内部布局、索引和控制器创建了一个MyProfileComponent 以在 /toolkit 中休息。我还创建了一个 ProfileComponent 驻留在 /toolkit/profile/:uniquename

问题是,当我在添加内部路由后运行应用程序时,出现错误。

/toolkit 抛出此错误

异常:调用节点模块失败并出现错误:错误:未捕获(承诺中):错误:找不到加载“ProfileComponent”的主要出口

错误:找不到加载“ProfileComponent”的主要出口

在 getOutlet (D:\Projects..\node_modules\@angular\router\bundles\router.umd.js:3018:23)

和 /toolkit/profile/sdfsad 抛出这个错误

异常:调用节点模块失败并出现错误:错误:未捕获(承诺中):[object Object]

我是 angular 2 的新手,我所看到的一切都希望您在组件上使用现在已过时的 Directives 属性,但是在这个版本的 angular 2 中已经消失了(@angular 是 2.0.0 版)。我可能做错了什么,但这里是碎片:

_InternalLayout(去掉多余的东西)

<head>
    <base href="/toolkit" />
</head>
<body class="menubar-top menubar-dark theme-primary">
    @RenderBody()

    @RenderSection("scripts", required: false)
</body>

/Toolkit/Index.cshtml

@{
    Layout = "~/Views/Shared/_InternalLayout.cshtml";
}

<app asp-prerender-module="@Url.Content("ClientApp/dist/main-server")">Loading...</app>

<script src="@Url.Content("~/dist/vendor.js")" asp-append-version="true"></script>

@section scripts {
    <script src="@Url.Content("~/dist/main-client.js")" asp-append-version="true"></script>
}

app.component.html

<router-outlet></router-outlet>

app.component.ts

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

@Component({
    selector: 'app',
    template: require('./app.component.html'),
})
export class AppComponent {

}

app.module.ts

import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { UniversalModule } from 'angular2-universal';

import { AppComponent } from './components/app/app.component'
import { LandingPageLayoutComponent } from './components/landing/landing-page-layout.component';
import { SignInComponent } from './components/account/sign-in.component';
import { RegisterComponent } from './components/account/register.component';
import { ProfileComponent } from './components/profile/profile.component';
import { MyProfileComponent } from './components/profile/my-profile.component';

@NgModule({
    bootstrap: [ AppComponent ],
    declarations: [
        AppComponent,
        LandingPageLayoutComponent,
        SignInComponent,
        RegisterComponent,
        ProfileComponent,
        MyProfileComponent
    ],
    imports: [
        UniversalModule, // Must be first import. This automatically imports BrowserModule, HttpModule, and JsonpModule too.
        RouterModule.forRoot([
            { path: 'signin', component: SignInComponent },
            { path: 'register', component: RegisterComponent },

            {
                path: 'toolkit',
                component: MyProfileComponent,
                children: [
                    { path: '' },
                    { path: 'profile/:uniquename', component: ProfileComponent },
                ]
            },

            { path: '', component: LandingPageLayoutComponent },
            { path: '**', redirectTo: 'error' }
        ])
    ]
})
export class AppModule {
}

根页面、登录和注册所有工作,尽管当我尝试在 /account 下登录和注册孩子时遇到了同样的问题。我确定我在做一些愚蠢的事情,一旦我们弄清楚了,我会尝试在这些页面上进行其他嵌套。

编辑

如果重要的话,我正在使用this Visual Studio 2015 template。在我开始尝试添加嵌套路由之前,它工作正常。

【问题讨论】:

    标签: angular asp.net-core angular2-routing


    【解决方案1】:

    根据您的错误以及我在 app.module.ts 中的 RouterModule 代码中看到的内容,根据您的目标,有两种可能的解决方案。

    1) 如果在路径“/toolkit”,您想查看MyProfileComponent,而在路径“/toolkit/profile/:uniquename”(此处显示完整路径)您想查看ProfileComponent 而不是 MyProfileComponent 那么您的路由器的代码需要如下所示:

        RouterModule.forRoot([
            { path: 'signin', component: SignInComponent },
            { path: 'register', component: RegisterComponent },
            {
                path: 'toolkit',
                children: [ // change here
                    { path: '', component: MyProfileComponent }, // both components are siblings within 'toolkit' path
                    { path: 'profile/:uniquename', component: ProfileComponent }
                ]
            },
            { path: '', component: LandingPageLayoutComponent },
            { path: '**', component: ErrorComponent } // this also needs to be changed to match a component
        ])
    

    2) 如果您希望在路由 'toolkit/profile/:uniquename' 看到 MyProfileComponent 和 ProfileComponent 嵌套在其中,那么您的路由器代码应该如下所示

        RouterModule.forRoot([
            { path: 'signin', component: SignInComponent },
            { path: 'register', component: RegisterComponent },
    
            {
                path: 'toolkit',
                component: MyProfileComponent,
                children: [
                    // this line needs to be removed { path: '' },
                    { path: 'profile/:uniquename', component: ProfileComponent }
                ]
            },
            { path: '', component: LandingPageLayoutComponent },
            { path: '**', component: ErrorComponent } // this also needs to be changed to match a component
        ])
    

    (2) cont'd- 然后在my-profile.component.ts 内,你的template 必须有一个&lt;router-outlet&gt;&lt;/router-outlet&gt; 来处理嵌套的子路由

    在这两种情况下,您的path: **' 都需要有一个与之关联的组件。进行重定向时,您必须包含 pathMatch 属性,例如pathMatch: 'full'

    【讨论】:

    • 感谢您的回复,托马斯。这是我追求的第一个条件,两个是兄弟姐妹。我根据您的建议更改了路由,现在当我转到 /toolkit 时,我看到的是 LandingPageComponent 而不是 MyProfileComponent。 /toolkit/profile/asfsdf 向我展示了 ProfileComponent。我们更近了!
    • 在 index.html(或者更确切地说是 _InternalLayout)中,尝试将您的基本标记更改为 &lt;base ref="/"&gt;。我怀疑现在 MyProfileComponent 是通过“工具包/工具包”导航到的,因为您将“工具包”设置为基本 href
    • 我试过了,有同样的想法,但是它继续加载错误的布局页面。我正在寻找其他选项,因为我认为交换 mvc 布局行不通。我想我会尝试根据路线在角度内交易布局,希望我可以使用stackoverflow.com/questions/34881401/… 来改变身体
    猜你喜欢
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多