错误:(SystemJS)XHR错误(404 Not Found)加载服务

时间:2016-11-10 10:39:23

标签: angular

我目前正在尝试使用服务传递字符串。但是,在我的AppComponent我正在获得undefined服务。

这是错误:

Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:3000/services/main.service
    patchProperty/desc.set/wrapFn@http://localhost:3000/node_modules/zone.js/dist/zone.js:698:26
    ZoneDelegate.prototype.invokeTask@http://localhost:3000/node_modules/zone.js/dist/zone.js:265:21
    Zone.prototype.runTask@http://localhost:3000/node_modules/zone.js/dist/zone.js:154:28
    ZoneTask/this.invoke@http://localhost:3000/node_modules/zone.js/dist/zone.js:335:28

    Error loading http://localhost:3000/services/main.service as "../services/main.service" from http://localhost:3000/app/app.module.js
Stack trace:
(SystemJS) XHR error (404 Not Found) loading http://localhost:3000/services/main.service
    patchProperty/desc.set/wrapFn@http://localhost:3000/node_modules/zone.js/dist/zone.js:698:26
    ZoneDelegate.prototype.invokeTask@http://localhost:3000/node_modules/zone.js/dist/zone.js:265:21
    Zone.prototype.runTask@http://localhost:3000/node_modules/zone.js/dist/zone.js:154:28
    ZoneTask/this.invoke@http://localhost:3000/node_modules/zone.js/dist/zone.js:335:28

    Error loading http://localhost:3000/services/main.service as "../services/main.service" from http://localhost:3000/app/app.module.js

main.service.ts

import { Injectable }                   from '@angular/core';
import { Http, Headers, Response }      from '@angular/http';
import { Observable }                   from 'rxjs/Observable';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';

@Injectable()
export class MainService {
    private projectURL = 'http://localhost:64895/api/Process/ProcessFile';
    private headers = new Headers({'Content-Type': 'application/json; charset=utf-8'});

    constructor(private http: Http) { }

    public passFileUrl = (filePath: string): Observable<number> => {
        var json = JSON.stringify(filePath);
        return this.http.post(this.projectURL, json, {headers: this.headers})
                    .map((response: Response) => <number>response.json());
    }
}

app.component.ts

import { Component } from '@angular/core';
import { MainService } from '../services/main.service';

@Component({
  selector: 'converter-utility',
  templateUrl: './app/views/main.component.html',
  providers: [MainService],
  styleUrls: ['./app/views/css/main.component.css']
})

export class AppComponent {
  private workableFile: string;

  constructor(
    private mainService: MainService
    ) { }

  doProcess() {
    this.mainService.passFileUrl(this.workableFile)
                    .subscribe((data: any) => {
                      console.log(data);
                    });
  }
}

app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule }   from '@angular/forms';
import { AppComponent }  from './app.component';
import { MainService }   from '../services/main.service';
@NgModule({
  imports: [
    BrowserModule,
    FormsModule
  ],
  declarations: [
    AppComponent
  ],
  providers: [
        MainService,
    ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

systemjs.config.js

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: 'app',
      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
      '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: './main.js',
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });
})(this);

的package.json

{
  "name": "angular-quickstart",
  "version": "1.0.0",
  "scripts": {
    "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
    "lite": "lite-server",
    "tsc": "tsc",
    "tsc:w": "tsc -w"
  },
  "licenses": [
    {
      "type": "MIT",
      "url": "https://github.com/angular/angular.io/blob/master/LICENSE"
    }
  ],
  "dependencies": {
    "@angular/common": "~2.1.1",
    "@angular/compiler": "~2.1.1",
    "@angular/core": "~2.1.1",
    "@angular/forms": "~2.1.1",
    "@angular/http": "~2.1.1",
    "@angular/platform-browser": "~2.1.1",
    "@angular/platform-browser-dynamic": "~2.1.1",
    "@angular/router": "~3.1.1",
    "@angular/upgrade": "~2.1.1",
    "angular-in-memory-web-api": "~0.1.13",
    "core-js": "^2.4.1",
    "reflect-metadata": "^0.1.8",
    "rxjs": "5.0.0-beta.12",
    "systemjs": "0.19.39",
    "zone.js": "^0.6.25"
  },
  "devDependencies": {
    "@types/core-js": "^0.9.34",
    "@types/node": "^6.0.45",
    "concurrently": "^3.0.0",
    "lite-server": "^2.2.2",
    "typescript": "^2.0.3"
  }
}

应用结构

  app
   app.component.ts
   app.module.ts
   main.ts
  node_modules ...
  services
   main.service.ts
  index.html
  package.json
  styles.css
  systemjs.config.js
  tsconfig.json

我尝试了一些解决方案,例如this one,没有成功。我确实意识到有很多类似的问题(可以将这个问题归类为重复),但没有一个能帮助我解决它。 更不用说我现在使用Angular 2几天了,所以问题可能出在椅子和键盘之间。 ☺ 如果你能协助我做出指示,我很感激。

非常感谢提前!

2 个答案:

答案 0 :(得分:6)

决议:

移动应用程序文件夹中的服务文件夹,更正导入,使它们指向正确的位置,这将修复404错误。

使用app文件夹外的服务文件夹:

打开systemjs.config.js

添加:

var map = {
    'services' : '/services', 
};

答案 1 :(得分:2)

问题是from部分中指定的字符串应解析为文件。例如,以下import语句:

import { MainService } from '../services/main.service';

如果没有额外的配置,SystemJS会尝试加载文件'../services/main.service'(注意:没有扩展名)。

选项1:配置SystemJS为您的代码添加“.js”的默认扩展名。有关多种方法,请参阅SystemJS Configuration API reference

选项2:在导入语句中指定.js扩展名。这是最初讨论{4}}的TypeScript功能。

就个人而言,我更喜欢第一种选择。