Angular 2和Asp MVC 5中的路由

时间:2017-03-11 10:11:35

标签: asp.net angularjs asp.net-mvc angular angular2-routing

我在asp mvc 5中使用了角度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";
}

它向我显示错误:

  

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

  

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

  

严重级代码描述项目文件行抑制状态   错误TS2305模块&#39;&#34; F:/ MyProject / angfinal / angfinal / node_modules / @ angular / router / index&#34;&#39;没有导出的成员&#39; ROUTER_PROVIDERS&#39;。 TypeScript虚拟项目F:\ MyProject \ angfinal \ angfinal \ app \ app.component.ts 3活动

  

严重级代码描述项目文件行抑制状态   错误TS2305模块&#39;&#34; F:/ MyProject / angfinal / angfinal / node_modules / @ angular / router / index&#34;&#39;没有导出的成员&#39; RouteConfig&#39;。 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";
}

和标题:

<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>

和config:

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 个答案:

答案 0 :(得分:1)

您似乎使用了较旧的angular 2路由器提供商和服务。如果您必须使用旧版本,则应使用router-deprecated。否则使用angular documentation

中描述的新语法