Search code examples
angulartypescriptangular2-services

Angular2 Custom Service not found


I am trying to use custom service for populating people list in my HTML. I've created custom service as per below:

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

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);

But I am getting error in console like this: enter image description here

Here is my project Structure in VS Code:
enter image description here


Solution

  • 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

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

    Or add a new package to your systemjs.config.js:

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