摘要
本文主要讲述了:
- 新建服务
- 将服务注入组件
正文
新建服务
示例:
- 在
src/app/
目录内创建名为services
的目录
- 在
src/app/services/
目录内创建jobs.service.ts
、jobs.service.spec.ts
1 2 3
| #!/usr/bin/env bash
ng generate service services/jobs
|
将服务注入组件
示例:
my-angular/src/app/app.component.html
1 2 3 4 5 6 7
| <div *ngFor="let item of jobList; trackBy: jobTrackBy"> <div>{{ item.jobId }}</div> <div>{{ item.jobName }}</div> <div>{{ item.salary }}</div> <div>{{ item.companyName }}</div> </div> <button (click)="getJobList()">Get Job List</button>
|
my-angular/src/app/app.component.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| import { Component } from '@angular/core'; import { Job } from './types/Job'; import { JobsService } from './services/jobs.service';
@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], }) export class AppComponent { jobList: Job[] = [];
jobTrackBy(index: number, item: Job) { return item.jobId; }
getJobList() { this.jobList = this.jobService.getList(); }
constructor(private jobService: JobsService) {} }
|
my-angular/src/app/services/jobs.service.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import { Injectable } from '@angular/core'; import { Job } from '../types/Job';
@Injectable({ providedIn: 'root', }) export class JobsService { getList(): Job[] { return [ { jobId: 1, jobName: '会计', salary: 1000, companyName: 'A有限责任公司', isVip: 1, }, { jobId: 2, jobName: '销售', salary: 2000, companyName: 'B有限责任公司', isVip: 0, }, { jobId: 3, jobName: '主管', salary: 3000, companyName: 'B有限责任公司', isVip: 0, }, ]; }
constructor() {} }
|
示例:跨组件使用服务
my-angular/src/app/app.component.html
1 2 3 4 5 6
| <app-login-form></app-login-form>
<div> <p>{{ loginService.userInfo["username"] }}</p> <p>{{ loginService.userInfo["password"] }}</p> </div>
|
my-angular/src/app/app.component.ts
1 2 3 4 5 6 7 8 9 10 11
| import { Component } from '@angular/core'; import { LoginService } from './services/login.service';
@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], }) export class AppComponent { constructor(public loginService: LoginService) {} }
|
my-angular/src/app/foo/foo.component.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <div> <label for="username">username: </label> <input type="text" name="username" id="username" [value]="loginService.userInfo['username']" (input)="onInput($event)" />
<label for="password">password: </label> <input type="password" name="password" id="password" [value]="loginService.userInfo['password']" (input)="onInput($event)" /> </div>
|
my-angular/src/app/components/login-form.component.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import { Component, OnInit } from '@angular/core'; import { LoginService } from 'src/app/services/login.service';
@Component({ selector: 'app-login-form', templateUrl: './login-form.component.html', styleUrls: ['./login-form.component.css'], }) export class LoginFormComponent implements OnInit { onInput(event: Event) { const target = event.target as HTMLInputElement; this.loginService.updateUserInfo(target.name, target.value); }
constructor(public loginService: LoginService) {}
ngOnInit(): void {} }
|
my-angular/src/app/types/LoginInfo.ts
1 2 3 4 5
| interface LoginInfo { [key: string]: string; }
export { LoginInfo };
|
my-angular/src/app/services/login.service.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import { Injectable } from '@angular/core'; import { LoginInfo } from '../types/LoginInfo';
@Injectable({ providedIn: 'root', }) export class LoginService { userInfo: LoginInfo = { username: '', password: '', };
updateUserInfo(key: string, name: string) { this.userInfo[key] = name; }
constructor() {} }
|
参考资料
本文对你有帮助?请支持我