6、新建City类
CitiesAngularApp下打开终端后,运行如下命令
ng g class models\City
生成的文件如下
city.ts
export class City {
cityID: string | null;
cityName: string | null;
constructor(cityID: string | null = null, cityName: string | null = null) {
this.cityID = cityID;
this.cityName = cityName;
}
}
7、新建CitiesService
CitiesAngularApp下打开终端后,运行如下命令
ng g service services\Cities
生成的文件如下
cities.service.ts
import { Injectable } from '@angular/core';
import { City } from '../models/city'
@Injectable({
providedIn: 'root'
})
export class CitiesService {
cities: City[] = [];
constructor() {
this.cities = [
new City("101", "New York"),
new City("102", "New Delhi"),
new City("103", "Sydney"),
new City("104", "Berlin"),
];
}
public getCities(): City[] {
return this.cities;
}
}
Gitee获取源码: