简体   繁体   English

找不到Angular2自定义服务

[英]Angular2 Custom Service not found

I am trying to use custom service for populating people list in my HTML. 我正在尝试使用自定义服务来填充HTML中的人员列表。 I've created custom service as per below: 我已经根据以下内容创建了自定义服务:

app.peopleListService.ts 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},
    ];
  }
}

I've register this provider in my module.ts 我已经在我的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 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 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);

But I am getting error in console like this: 但是我在控制台中遇到这样的错误: 在此处输入图片说明

Here is my project Structure in VS Code: 这是我在VS Code中的项目结构:
在此处输入图片说明

You should either move your services folder into your app folder, which makes a lot more sense to do so, and change the import to 您应该将services文件夹移到您的app文件夹中,这样做更有意义,然后将导入更改为

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

Or add a new package to your systemjs.config.js : 或向您的systemjs.config.js添加一个新包:

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

I think it's "./services/app.peopleListService" 我认为它是"./services/app.peopleListService"

module.ts module.ts

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM