错误:XHR错误(404 Not Found)loading http:// localhost:3622 / app.js(...)+ angular 2

时间:2016-05-25 11:25:00

标签: angularjs angular

我正在尝试使用visual studio 2015应用程序中的typescript构建一个角度2应用程序。我正在使用角度释放候选人1。

运行应用程序时,我在浏览器的控制台中收到以下错误消息。

index.html:24错误:错误:XHR错误(404 Not Found)loading http://localhost:3622/app.js(...)

我无法理解它为什么要查找app.js文件。我已将index.html设置为起始页。

如下面的代码所示,我试图在数据表中显示数据。我是如何设置systemjs.js文件或index.html文件中的system.import语句的?

请找到我的文件夹结构

enter image description here

的index.html

<!DOCTYPE html>
<html>

<head>
    <title>Angular 2 Application</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>


    <!-- 1. Load libraries -->
    <!-- Polyfill(s) for older browsers -->
    <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="node_modules/primeui/primeui-ng-all.min.js"></script>

    <!-- 2. Configure SystemJS -->
    <script src="systemjs.config.js"></script>
    <script>
        System.import('app').catch(function (err) { console.error(err); });
    </script>

</head>

<body>
    <my-app>Loading..</my-app>
</body>

</html>

Systemjs.config.js

(function (global) {

    // map tells the System loader where to look for things
    var map = {
        'app': 'app', // 'dist',
        'rxjs': 'node_modules/rxjs',
        'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
        '@angular': 'node_modules/@angular',
        'primeng': 'node_modules/primeng'
    };

    // packages tells the System loader how to load when no filename and/or no extension
    var packages = {
        '.': { main: 'main.js', defaultExtension: 'js' },
        'rxjs': { defaultExtension: 'js' },
        'angular2-in-memory-web-api': { defaultExtension: 'js' },
    };

    var packageNames = [
      '@angular/common',
      '@angular/compiler',
      '@angular/core',
      '@angular/http',
      '@angular/platform-browser',
      '@angular/platform-browser-dynamic',
      '@angular/router',
      '@angular/testing',
      '@angular/upgrade',
      '@angular/router-deprecated'
    ];

    // add package entries for angular packages in the form '@angular/common': { main: 'index.js', defaultExtension: 'js' }
    packageNames.forEach(function (pkgName) {
        packages[pkgName] = { main: 'index.js', defaultExtension: 'js' };
    });

    var config = {
        map: map,
        packages: packages
    }

    // filterSystemConfig - index.html's chance to modify config before we register it.
    if (global.filterSystemConfig) { global.filterSystemConfig(config); }

    System.config(config);

})(this);

main.ts

import { bootstrap } from '@angular/platform-browser-dynamic';

// Our main component
import { AppComponent } from './app/app.component';


bootstrap(AppComponent, []).catch(err => console.error(err));

app.component.ts

import { Component } from '@angular/core';
import { RiskListComponent } from './components/risks/risk-list.component';
import { DataTable, Column } from 'primeng/primeng';


@Component({
    selector: 'my-app',
    providers: [],
    template: `
    <div>
        <h1>{{pageTitle}}</h1>
          <rm-risks> </rm-risks> 
     </div>
     ` ,
    directives: [RiskListComponent, DataTable, Column]

})

export class AppComponent {
    pageTitle: string = 'Test UK Trader'; 
}

risk-list.component.ts

import { Component } from '@angular/core'
import { DataTable, Column } from 'primeng/primeng';

@Component({
    selector: 'rm-risks',
    directives: [DataTable, Column],
    templateUrl: '/app/risks/risk-list.component.html'



    })

export class RiskListComponent {
    pageTitle: string = 'Risk List';
    risks: any[] = [
        {
            "riskId": 1,
            "reference": "HISC9308336",
            "insuredName": "SA 84161",
            "inceptionDate": "March 19, 2016",
            "riskType": "Quote",
            "status": "Indication",
            "grossPremium": "100",
            "allocatedTo": "Broker User",
            "allocatedCompany": "Broker"
        },
        {
            "riskId": 2,
            "reference": "HISC9308340",
            "insuredName": "Upper Loi",
            "inceptionDate": "April 25, 2016",
            "riskType": "Quote",
            "status": "Indication",
            "grossPremium": "312.22",
            "allocatedTo": "Andy Marples",
            "allocatedCompany": "Broker"
        }
    ];
}

风险list.component.html

<h3 class="first">{{pageTitle}}</h3>

<p-dataTable [value]="risks" [rows]="10" [paginator]="true" [pageLinks]="3" [rowsPerPageOptions]="[5,10,20]">
    <p-column field="reference" header="Reference"></p-column>
    <p-column field="insuredName" header="Insured Name"></p-column>
    <p-column field="inceptionDate" header="Inception Date"></p-column>
    <p-column field="riskType" header="Risk Type"></p-column>
    <p-column field="status" header="Status"></p-column>
    <p-column field="grossPremium" header="Gross Premium"></p-column>
    <p-column field="allocatedTo" header="Allocated To"></p-column>
    <p-column field="allocatedCompany" header="Allocated Company"></p-column>
</p-dataTable>

tsconfig.js

{
  "compileOnSave": false,
  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "noEmitOnError": true,
    "noImplicitAny": false,
    "outDir": "../dist/",
    "rootDir": ".",
    "sourceMap": true,
    "target": "es6",
    "inlineSources": true
  },
  "exclude": [
    "node_modules",
    "typings",
    "typings/main",
    "typings/main.d.ts"

  ]
}

1 个答案:

答案 0 :(得分:3)

我会将main.ts文件移到app文件夹下并以这种方式导入:

<script>
  System.import('app/main').catch(function (err) { console.error(err); });
</script>

您还需要为SystemJS配置此配置:

var packages = {
    'app': { main: 'main.js', defaultExtension: 'js' },
    'rxjs': { defaultExtension: 'js' },
    'angular2-in-memory-web-api': { defaultExtension: 'js' },
};