当我尝试在user.service.ts中设置属性时,我遇到了类型错误。 这就是user.service的样子:
import { Injectable } from '@angular/core';
import { UserModel } from '../models/user.model';
@Injectable()
export class UserService {
private _user: UserModel;
constructor() {}
get getUser(): UserModel {
return this._user;
}
set setUser(user: UserModel) {
this._user = user;
}
}
和UserModel:
export class UserModel {
constructor(public uid: string,
public displayName: string,
public email: string,
public photoUrl: string,
public providerId: string) {}
}
最后,这是我收到错误的auth.service。
import {Injectable} from '@angular/core';
import * as firebase from 'firebase';
import { AngularFireAuth } from 'angularfire2/auth';
import {Router} from '@angular/router';
import { UserModel } from '../models/user.model';
import { UserService } from './user.service';
@Injectable()
export class AuthService {
private _token: string = null;
// We use first login in app.module to check if is the first login. If it is we skip the refresh token method.
private _firstLogin = false;
constructor(private afAuth: AngularFireAuth,
private router: Router,
private userService: UserService) {}
get isFirstLogin() {
return this._firstLogin;
}
get getUserToken(): string{
return this._token;
}
set setUserToken(tk: string) {
this._token = tk;
}
// We define the Facebook provider and passing it to signin(). We do this for each provider that we want to integrate.
signinWithFacebook(): Promise<any> {
const fbProvider = new firebase.auth.FacebookAuthProvider();
return this.signin(this.afAuth.auth.signInWithPopup(fbProvider));
}
// If this method get resolved then we redirect the user to the home page and get the token.
// Besides, when this method execute the reject() we catch it in the login component and we handle the errors there.
// This method can be reusable across multiple providers such Facebook, Twitter, Github , etc.
signin(popupResult: Promise<any>): Promise<any> {
return popupResult
.then(
(res) => {
this._firstLogin = true;
const user: firebase.User = res.user.toJSON();
const credential = res.credential;
this._token = credential.accessToken;
// Initialising the user and passing to the user service's property (_user)
// TODO fix: Solucionar error de la línea 51.
const providedData = user.providerData[0];
const buildedUser = new UserModel(providedData.uid, providedData.displayName,
providedData.email, providedData.photoURL, providedData.providerId);
this.userService.setUser(buildedUser); //HERE I GOT THE ERROR.
console.log(this._token);
console.log(user);
}
);
}
}
当我在这行代码中尝试将UserModel从auth.service传递给user.service时,我收到了错误:this.userService.setUser(buildedUser)。 我希望你能得到它并给我一个解决方案,并告诉我为什么会发生这种情况。 此致!
答案 0 :(得分:2)
从get
中的方法中删除set
和UserService
:
@Injectable()
export class UserService {
private _user: UserModel;
constructor() {}
getUser(): UserModel {
return this._user;
}
setUser(user: UserModel) {
this._user = user;
}
}
您可能不想对setUser()
使用setter。如果您执行this.userService.setUser = buildedUser
之类的操作,将会调用setter。如果您想将其称为this.userService.setUser(buildedUser)
,那么setUser()
只是一个简单的旧方法。
同样,当您收到该用户时,您可能想要致电const retrievedUser = this.userService.getUser()
,而不是const retrievedUser = this.userService.getUser
。前者只是一种简单的方法,而后者则是一种吸气剂。
如果您真的想使用getter和setter,可以保留get
和set
,但可以将getUser
和setUser
重命名为user
:< / p>
@Injectable()
export class UserService {
private _user: UserModel;
constructor() {}
get user(): UserModel {
return this._user;
}
set user(user: UserModel) {
this._user = user;
}
}
然后拨打this.userService.user = buildedUser
或const retrievedUser = this.userService.user
,即可调用您的setter和getter。
但是getter和setter背后的想法是调用者将它们视为属性访问,而你可以将它们实现为不仅仅是获取和设置属性的方法。如果你所做的只是获取和设置属性,你可能只有一个名为user
的公共属性:
@Injectable()
export class UserService {
public user: UserModel;
constructor() {}
}
这样可以让您this.userService.user = buildedUser
或const retrievedUser = this.userService.user
更轻松地完成。