【发布时间】:2019-02-14 01:38:48
【问题描述】:
我正在使用 Angular 7。我想在 typescript 中获取和设置变量
我的代码 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;
}
}
我在 Login.Component.ts 中的代码
fLogin(user, pass) {
this.users.setUser=user;
}
还有我在 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();
}
}
我希望在 Login.Component.ts 中设置值并在 Customer.Component.ts
中获取值它如何工作或其他工作?
【问题讨论】:
标签: angular typescript getter-setter