我想在我的user.service.ts中設置屬性時出現類型錯誤。 這是user.service的樣子:無法調用其類型缺少呼叫簽名的表達式。類型'UserModel'沒有兼容的呼叫簽名。
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);
}
);
}
}
我,當我試圖從auth.service傳遞一個的usermodel在這行代碼user.service錯誤:this.userService.setUser(buildedUser)。 我希望你得到它並給我一個解決方案,並告訴我爲什麼會發生這種情況。 關心!
謝謝。我要用一個setter來使用它,但是你已經澄清它很棒。 –
我已經將用戶設置爲私有,因爲多態。 UserService幾乎被創建,稍後將會有更多的邏輯。 –