2016-02-08 359 views
0

我看到一些使用Angular 2的ASP.Net MVC中的typescript导入非常奇怪的行为。运行时报告它无法从根目录找到ng2-highcharts软件包 - 而不是node_modules。ASP.NET MVC Angular 2无法加载资源:服务器响应状态为404(未找到)

所有其他导入工作 - 相对于node_modules被识别。

代码:

import {Component, View, OnInit} from 'angular2/core'; 
import {Ng2Highcharts} from "ng2-highcharts/ng2-highcharts"; 
import {CORE_DIRECTIVES, FORM_DIRECTIVES, NgClass} from 'angular2/common'; 
import EndPointService from '../services/EndPointService'; 

//import {CHART_DIRECTIVES} from 'ng2-charts'; 

@Component({ 
    selector: 'index', 
    providers: [EndPointService], 
    templateUrl: '/Admin.Portal/App/views/view-admin-users.html', 
    directives: [Ng2Highcharts] 
}) 
export class IndexComponent ... 

错误:XHR错误(404未找到)装载http://localhost/Admin.Portal/ng2-highcharts/ng2-highcharts.js(...)

运行@ angular2-polyfills.js:138

tsConfig:

{ 
    "compilerOptions": { 
    "module": "system", 
    "sourceMap": true, 
    "noImplicitAny": true 
    } 
} 

.csproj打字稿设置

<TypeScriptTarget>ES5</TypeScriptTarget> 
    <TypeScriptJSXEmit>None</TypeScriptJSXEmit> 
    <TypeScriptCompileOnSaveEnabled>True</TypeScriptCompileOnSaveEnabled> 
    <TypeScriptNoImplicitAny>False</TypeScriptNoImplicitAny> 
    <TypeScriptModuleKind>CommonJS</TypeScriptModuleKind> 
    <TypeScriptRemoveComments>False</TypeScriptRemoveComments> 
    <TypeScriptOutFile /> 
    <TypeScriptOutDir /> 
    <TypeScriptGeneratesDeclarations>False</TypeScriptGeneratesDeclarations> 
    <TypeScriptNoEmitOnError>True</TypeScriptNoEmitOnError> 
    <TypeScriptSourceMap>True</TypeScriptSourceMap> 
    <TypeScriptMapRoot /> 
    <TypeScriptSourceRoot /> 
    <TypeScriptExperimentalDecorators>True</TypeScriptExperimentalDecorators> 

我试过导入相对路径,但后来tsc失败。谁能帮忙?

在此先感谢。

+0

你有一个'<基本href = “/”>'在你的''元素或'引导相当于提供商()'? –

+0

嗨,谢谢你的回复。我的确有这样的想法。我不明白为什么只有HighChart导入不会给我一个来自node_modules的相对路径。 – KnowHoper

+0

你的配置有问题,你应该把它添加到问题中。 –

回答

1

您应该为此添加一个映射条目到您的SystemJS配置中。事情是这样的:

<script> 
    System.config({ 
    map: { 
     'ng2-highcharts': 'node_modules/ng2-highcharts', 
    }, 
    packages: { 
     (...) 
    } 
    }); 
    (...) 
</script> 
相关问题