简体   繁体   English

如何在 Angular 中创建 Getter Setter

[英]How To Create Getter Setter In Angular

I'm using Angular 7. I want to get and set variable in typescript我正在使用 Angular 7。我想在打字稿中获取和设置变量

My Code login.service.ts我的代码 login.service.ts

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class LoginService {
  public username:string;
  public token:string;
  public fullname:string;
  constructor() { }
  set setUser(val: string){
    this.username = val;
  }
  set setToken(val:string){
    this.token=val;
  }
  set setFullName(val:string){
    this.fullname=val;
  }
  get getUser():string{
    return this.username;
  }
  get getToken():string{
    return this.token;
  }
  get getFullName():string{
    return this.fullname;
  }
}

And My Code In Login.Component.ts和我在 Login.Component.ts 中的代码

fLogin(user, pass) {
    this.users.setUser=user;
}

And My Code In Customer.Component.ts还有我在 Customer.Component.ts 中的代码

import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { Http, Headers } from "@angular/http";
import {LoginService} from "../login.service"
import { NgLocalization } from '@angular/common';
@Component({
  selector: 'app-customers',
  templateUrl: './customers.component.html',
  styleUrls: ['./customers.component.css']
})
export class CustomersComponent implements OnInit {
  constructor(public user:LoginService) {

  }
  loadData() {
    console.log(this.user.getUser)
  }
    ngOnInit() {
    this.loadData();
  }
}

I expect Set Value In Login.Component.ts And Get Value in Customer.Component.ts我希望在Login.Component.ts 中设置值并在Customer.Component.ts 中获取值

How to it working or the other it working?如何工作或其他工作?

1) Ensure your login.component.ts file also injects the service. 1) 确保您的 login.component.ts 文件也注入了该服务。

constructor(public user:LoginService)

2) Ensure that no module or component has a providers array containing your service. 2) 确保没有模块或组件具有包含您的服务的providers数组。

If you register the service using the providedIn: 'root' as you have, and don't register the service again in any providers array, then the service should be a singleton and work as you expect.如果您使用providedIn: 'root'注册服务,并且不在任何providers数组中再次注册该服务,那么该服务应该是一个单例并按您的预期工作。

Also, the naming of your getters and setters is incorrect.此外,您的 getter 和 setter 的命名不正确。 The getter and setter should have the same name, as they are just another way to define a property: getter 和 setter 应该具有相同的名称,因为它们只是定义属性的另一种方式:

  get user():string{
    return this.username;
  }
  set user(val: string){
    this.username = val;
  }
  get token():string{
    return this.token;
  }
  set token(val:string){
    this.token=val;
  }
  get fullName():string{
    return this.fullname;
  }
  set fullName(val:string){
    this.fullname=val;
  }

You then access those getter/setter properties just like any other declared properties.然后,您可以像访问任何其他声明的属性一样访问这些 getter/setter 属性。

 this.fullName = "Joe Smith"; // to set the value and call the setter

 console.log(this.fullName); // to reference the value.

In your customer component:在您的客户组件中:

  constructor(public loginService:LoginService) {

  }
  loadData() {
    console.log(this.loginService.user)
  }

NOTE: I renamed your constructor parameter to better identify it.注意:我重命名了您的构造函数参数以更好地识别它。

In your login component:在您的登录组件中:

  constructor(public loginService:LoginService) {

  }

  fLogin(user, pass) {
     this.loginService.user = user;
  }

you need only have 2 same functions, one with prefix set and another else with prefix get:你只需要 2 个相同的函数,一个带有前缀集,另一个带有前缀 get:

****in the Service User

private name: string;

public get info() {
  return name;
}

public set info(val) {
  this.name = val;
}


************ usage in other components or services
this.User.info = 'your name'; // for set

const yourName = this.User.info; // for get

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM