【问题标题】:Error: (SystemJS) XHR error (404 Not Found) loading service错误:(SystemJS)XHR 错误(404 Not Found)加载服务
【发布时间】:2016-11-10 10:39:22
【问题描述】:

我目前正在尝试使用服务来传递字符串。但是,在我的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

ma​​in.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 几天了,所以问题可能出在椅子和键盘之间。 ☺ 如果您能帮我指路,我将不胜感激。

提前非常感谢!

【问题讨论】:

  • 你也可以发布你的 package.json 吗?
  • 嗨@AvramVirgil,刚刚编辑并添加了package.jon和systemjs.config.js
  • Service 文件夹是否位于您的应用文件夹之外?
  • 是的,@AvramVirgil。
  • 组件、指令、服务、管道应该在app文件夹内,css、字体、图像等资产可以在app文件夹外

标签: angular


【解决方案1】:

分辨率:

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

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

打开 systemjs.config.js

添加:

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

【讨论】:

  • hI @Avram VIrgil,如果您可以根据我们关于应用程序结构的对话编辑此答案,我会接受。事实上,通过在应用程序内移动服务文件夹解决了这个问题。此外,我很想解释为什么我的结构失败了。谢谢!
  • 你可以使用app文件夹外的文件夹但是你需要修改systemjs.config.js让angular知道那个文件夹的存在
【解决方案2】:

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

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

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

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

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

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

【讨论】:

  • 嗨 Ronald,systemjs.config.js 已经将扩展名默认为“js”,这满足了您的选项 1。尝试了选项 2,但没有运气。
  • @AuroMetal 如果服务文件夹在 app 文件夹之外,您当前的 SystemJS 配置不会为它们设置默认扩展名 - 它们仅为 app 文件夹设置。
猜你喜欢
  • 2017-03-16
  • 2017-07-28
  • 2017-07-09
  • 1970-01-01
  • 2017-01-20
  • 1970-01-01
  • 1970-01-01
  • 2018-01-14
  • 2019-12-09
相关资源
最近更新 更多