1

I have created HeroApp in which I display list of Heros from a Service, following this tutorial.

When the user selects any Hero, details of that particular Hero are displayed. However, when I manually append the hero id to url as follows, I get an error:

GET http://localhost:3000/persons/node_modules/core-js/client/shim.min.js
GET http://localhost:3000/persons/systemjs.config.js
GET http://localhost:3000/persons/node_modules/zone.js/dist/zone.js
GET http://localhost:3000/persons/node_modules/reflect-metadata/Reflect.js
GET http://localhost:3000/persons/node_modules/systemjs/dist/system.src.js
GET http://localhost:3000/persons/systemjs.config.js
Uncaught ReferenceError: System is not defined

Here is my code:

app.personList.ts:

import { Component } from '@angular/core';
import { Person } from "../model/peopleModel";
import { PeopleService } from "../services/app.peopleListService";

@Component({
  selector: 'app-people-list',
  templateUrl: './peoplelist/app.peopleList.html'
})
export class PeopleListComponent {
  people: Person[] = [];
  selectedPerson: Person;

  constructor(peopleService : PeopleService){
    this.people = peopleService.getAll();
  }

  personSelect(person : Person)
  {
    this.selectedPerson = person;
  }
}

app.personList.html

<ul>
    <ul>
    <li *ngFor="let person of people">
        <a [routerLink]="['/persons', person.id]">
            {{person.name}}
        </a>
    </li>
</ul>

When the user clicks on a hero it shows the details of the Hero and the url changes to:

http://localhost:3000/persons/2

app.personDetail.ts:

import { Component, Input, OnInit, OnDestroy } from "@angular/core";
import { Person } from "../model/peopleModel";
import { PeopleService } from "../services/app.peopleListService";
import { ActivatedRoute, Router } from "@angular/router";

@Component({
  selector: 'app-person-details',
  templateUrl: '/persondetail/app.peopleDetail.html'
})

export class PeopleDetail implements OnInit, OnDestroy{
    @Input() person : Person;
    sub: any;

    constructor(private peopleService: PeopleService,
               private route: ActivatedRoute, private router: Router){
    }
    ngOnInit(){
        this.sub = this.route.params.subscribe(params => {
          let id = Number.parseInt(params['id']);
          this.person = this.peopleService.get(id);
        });
    }

    ngOnDestroy(){
        if(!!this.sub){
        this.sub.unsubscribe();
      }
    }

    gotoPeoplesList(){
    let link = ['/persons'];    
      this.router.navigate(link);
  }
}

app.personDetail.html:

<section *ngIf="person">
    <h2>You selected: {{person.name}}</h2>
    <h3>Description</h3>
    <p>
       {{person.name}} weights {{person.weight}} and is {{person.height}} tall.
    </p>
</section>

<button (click)="gotoPeoplesList()">Back to peoples list</button>

routing.ts:

import { PeopleListComponent } from "./peoplelist/app.peopleList";
import { Routes, RouterModule } from '@angular/router';
import { PeopleDetail } from "./persondetail/app.peopleDetail";

const routes: Routes = [
  // map '/persons' to the people list component
  {
    path: 'persons',
    component: PeopleListComponent,
  },
  // map '/' to '/persons' as our default route
  {
    path: 'persons/:id',
    component: PeopleDetail
  },
  {
    path: '',
    redirectTo: '/persons',
    pathMatch: 'full'
  },
];

export const appRouterModule = RouterModule.forRoot(routes);

enter image description here

3
  • what do you mean manually adding? can you please explain more specific, by the way does person with 1 exist? Commented Aug 4, 2017 at 15:24
  • manually means, if I type manualy in url. <a> tag click event. If I click a tag redirect works perfectly. Commented Aug 8, 2017 at 5:44
  • Getting the same issue in Angular 6 application. did you find any way to fix? Commented Sep 15, 2018 at 21:04

2 Answers 2

4

The index.html must have <base href="/"> before scripts.

Sign up to request clarification or add additional context in comments.

Comments

1

I got the same issue in Angular 6, when pass the parameter like bid/12345(bid/:id).

resolve it with replace

<base href="/">

with

<base href="./">

save my day.

1 Comment

No! in my condition, replacing href="./" to href="/" solved my problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.