找不到Angular2自定义服务

时间:2017-07-25 09:41:06

标签: angular typescript angular2-services

我正在尝试使用自定义服务在HTML中填充人员列表。我已经按照以下方式创建了自定义服务:

app.peopleListService.ts

import { Injectable } from '@angular/core';
import { Person } from "../model/peopleModel";


@Injectable()
export class PeopleService {
  constructor() { }

  getAll() : Person[] {
    return [
      {name: 'Luke Skywalker', height: 177, weight: 70},
      {name: 'Darth Vader', height: 200, weight: 100},
      {name: 'Han Solo', height: 185, weight: 85},
    ];
  }
}

我已在我的module.ts中注册此提供商

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

的package.json

{
  "name": "angular-demo",
  "version": "1.0.0",
  "scripts": {
    "start": "tsc -w"
  },
  "dependencies": {
    "@angular/common": "~2.3.0",
    "@angular/compiler": "~2.3.0",
    "@angular/core": "~2.3.0",
    "@angular/forms": "~2.3.0",
    "@angular/http": "~2.3.0",
    "@angular/platform-browser": "~2.3.0",
    "@angular/platform-browser-dynamic": "~2.3.0",
    "@angular/router": "~3.3.0",
    "angular-in-memory-web-api": "~0.2.1",
    "systemjs": "0.19.40",
    "core-js": "^2.4.1",
    "reflect-metadata": "^0.1.8",
    "rxjs": "5.0.0-rc.4",
    "zone.js": "^0.7.2"
  },
  "devDependencies": {
    "lite-server": "^2.3.0",
    "typescript": "~2.0.10"
  }
}

Systemjs.config.js

(function (global) {
  System.config({
    paths: {
      'npm:': 'node_modules/'
    },
    map: {
      myApp: 'app',
      '@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',
      'rxjs': 'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',
    },
    packages: {
      myApp: {
        main: './main.js',
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      },
      'angular-in-memory-web-api': {
        main: './index.js',
        defaultExtension: 'js'
      }
    }
  });
})(this);

但我在控制台中收到错误,如下所示: enter image description here

这是VS代码中的项目结构:
enter image description here

2 个答案:

答案 0 :(得分:2)

您应该将services文件夹移动到app文件夹中,这样做更有意义,并将导入更改为

import { PeopleService } from "./services/app.peopleListService";

或者向systemjs.config.js添加新套餐:

packages: {
  services: {
    defaultExtension: 'js'
  },

答案 1 :(得分:0)

我认为这是"./services/app.peopleListService"

<强> module.ts

import { PeopleService } from "./services/app.peopleListService";