首页 文章

Angular 2和Asp MVC 5中的路由

提问于
浏览
0

我在asp mvc 5中使用angular 2 .

我需要在视图中显示 <my-app> </my-app> 内容 .

app.component:

import { Component } from '@angular/core';
import 'rxjs/Rx';   // Load all features
import { ROUTER_PROVIDERS, RouteConfig, ROUTER_DIRECTIVES } from '@angular/router';

import { OtherComponent } from './other/other.component';

@Component({
    selector: 'my-app',
    template: `<h1>My Name is {{name}}</h1>
    <other-app></other-app>`,
})
@RouteConfig([
    { path: '/other', name: 'other', component: OtherComponent }
])
export class AppComponent {

    name = "Kianoush";
}

它向我显示错误:

严重级代码描述项目文件行抑制状态错误构建:模块'“F:/ MyProject / angfinal / angfinal / node_modules / @ angular / router / index”'没有导出成员'ROUTER_PROVIDERS' . angfinal F:\ MyProject \ angfinal \ angfinal \ app \ app.component.ts 3

.

严重级代码描述项目文件行抑制状态错误构建:模块'“F:/ MyProject / angfinal / angfinal / node_modules / @ angular / router / index”'没有导出成员'RouteConfig' . angfinal F:\ MyProject \ angfinal \ angfinal \ app \ app.component.ts 3

.

严重级代码说明项目文件行抑制状态错误TS2305模块'“F:/ MyProject / angfinal / angfinal / node_modules / @ angular / router / index”'没有导出成员'ROUTER_PROVIDERS' . TypeScript虚拟项目F:\ MyProject \ angfinal \ angfinal \ app \ app.component.ts 3有效

.

严重级代码说明项目文件行抑制状态错误TS2305模块'“F:/ MyProject / angfinal / angfinal / node_modules / @ angular / router / index”'没有导出成员'RouteConfig' . TypeScript虚拟项目F:\ MyProject \ angfinal \ angfinal \ app \ app.component.ts 3有效

.

other.component:

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

@Component({
    selector: 'other-app',
    templateUrl: './app/other/other.component.html'
})
export class OtherComponent {
    name = "kianoush";
}

和 Headers :

<head>
<base href="/">
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
<link href="~/Content/Site.css" rel="stylesheet" type="text/css" />
<link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="~/Scripts/modernizr-2.6.2.js"></script>
<script src="/node_modules/core-js/client/shim.min.js"></script>
<script src="/node_modules/zone.js/dist/zone.js"></script>
<script src="/node_modules/reflect-metadata/Reflect.js"></script>
<script src="/node_modules/systemjs/dist/system.src.js"></script>
<script src="/systemjs.config.js"></script>
<script>
  System.import('app').catch(function(err){ console.error(err); });
</script>

和配置:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
        routes.MapRoute(
            name: "NotFound",
            url: "{*catchall}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

如何在View中显示 other.component.html ,我该如何解决这个问题?

1 回答

  • 1

    您似乎使用了较旧的angular 2路由器提供商和服务 . 如果你必须使用旧版本,你应该使用router-deprecated . 否则使用angular documentation中描述的新语法

相关问题