【发布时间】:2019-04-14 10:09:05
【问题描述】:
当我尝试在 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;
}
}
和用户模型:
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)。 我希望你能得到它并给我一个解决方案,并告诉我为什么会发生这种情况。 问候!
【问题讨论】:
标签: angular typescript