在angualr 6中使用sharedObject服务的最佳方法是什么?

时间:2019-01-31 09:20:42

标签: angular typescript

我正在尝试找到在角度6中使用共享对象服务的最佳方法

要求是获取共享点登录用户的“详细信息”,“组”,“帐户名”和其他一些通常需要的信息。

目前正在将所有内容写入app.component.ts

import { Component, OnInit } from '@angular/core';
import { SPContextService } from './@core/spContext/spContext.service';
import { SharedObectService } from './@common/sharedObject.service';
import { IUser, ISPListCommon } from './@common/models';
import { Route, Router } from '@angular/router';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
 
  isAppLoading: boolean;

  constructor(private spDatacontext: SPContextService,
    private sharedObectService: SharedObectService,
    private router: Router) {

  }

  ngOnInit() {
    this.isAppLoading = true;

    this.spDatacontext.getCurentUser().subscribe(
      userdata => {

        this.sharedObectService.User = userdata as IUser;
        console.log(this.sharedObectService.User);
        this.isAppLoading = false;

      });




    this.spDatacontext.getUserRole(`(LoginName eq 'HRAdmins')`)
      .subscribe(
        data => {
          const isAdmin = data.value.length === 1 ? true : false;
          this.sharedObectService.IsAdmin = isAdmin;
        });


     this.spDatacontext.getListData('Title,Id', '', 'HRServiceCategories', '')`enter code here`
      .subscribe(categoriesData => {
        this.sharedObectService.Categories = categoriesData.value as Array<ISPListCommon>;
      });
  }

   
}

sharedObect.service.ts

import { IUser, ISPListCommon } from './models';
import { Injectable } from '@angular/core';

@Injectable({
    providedIn: 'root'
})

export class SharedObectService {

constructor() { }
private user: IUser; 
private isHRAdmin: boolean;
private CategoriesInfo: Array<ISPListCommon>;
private LocationsInfo: Array<ISPListCommon>;


/* Public getters and setters */
public set User(v: IUser) {
    this.user = v;
}

public get User(): IUser {
    return this.user;
}

public set IsAdmin(isAdmin: boolean) {
    this.isHRAdmin = isAdmin;
}
public get IsAdmin(): boolean {
    return this.isHRAdmin;
}

public set Categories(C: Array<ISPListCommon>) {
    this.CategoriesInfo = C;
}
public get Categories(): Array<ISPListCommon> {
    return this.CategoriesInfo;
}

}`

这是使用它的最佳方法吗?

0 个答案:

没有答案