6、angular服务
服务可以理解为公共的方法,项目里面需要用到的公共方法都可以抽离为一个公共的方法使用
服务类的定义通常紧跟在 “@Injectable()” 装饰器之后,依赖不一定是服务 —— 它还可能是函数或值。也就是说函数或值不需要@Injectable()装饰器
创建service的指令
ng generate service heroes/hero
下面给几个服务案例
1、公共函数或值,目录:app->core->services->utils->app-common.function.ts
export function getRole():string { return sessionStorage.getItem('erp_role'); }
2、公共服务,目录:app->core->services->services->dialog.service.ts
import { Injectable } from '@angular/core';
import { MatDialogRef, MatDialog } from '@angular/material';
import { MatConfirmDialogComponent } from 'src/app/shared/mat-confirm-dialog/mat-confirm-dialog.component';
@Injectable({
providedIn: 'root'
})
export class DialogService {
constructor(private dialog: MatDialog) { }
openConfirmDialog(msg, height){
console.log("message: " + msg + " height: " + height);
return this.dialog.open(MatConfirmDialogComponent,{
width: '390px',
panelClass: 'confirm-dialog-container',
disableClose: true,
position: { top: height+"px" },
data :{
message : msg
}
});
}
}

浙公网安备 33010602011771号