Interface AngularJS 2类型脚本接口
我有一个用于处理用户操作的服务和一个用于用户对象的接口 user.service.tsInterface AngularJS 2类型脚本接口,interface,typescript,angular,Interface,Typescript,Angular,我有一个用于处理用户操作的服务和一个用于用户对象的接口 user.service.ts import {Injectable} from 'angular2/core'; export interface User { name: string; email?: string; picture?: string; } @Injectable() export class UserService { me: User; constructor() { } se
import {Injectable} from 'angular2/core';
export interface User {
name: string;
email?: string;
picture?: string;
}
@Injectable()
export class UserService {
me: User;
constructor() {
}
setUser(user: User) {
this.me = user;
}
}
import {Injectable} from 'angular2/core';
declare const IN: any;
@Injectable()
export class LinkedinService {
constructor() {
IN.init({
api_key: 'xxxxxxxxxxx',
authorize: true
});
}
login() {
return new Promise((resolve, reject) => {
IN.User.authorize(() => resolve());
});
}
getMe() {
return new Promise((resolve, reject) => {
IN.API.Profile('me').result((profile) => resolve(profile.values[0]));
});
}
}
在我的登录组件中,我尝试使用从登录服务返回的配置文件设置用户,但出现以下错误:
类型“{}”上不存在属性“firstName”
login.component.ts
import {Component} from 'angular2/core';
import {User, UserService} from './services/user.service';
import {LinkedinService} from './services/linkedin.service';
declare const IN: any;
console.log('`Login` component loaded asynchronously');
@Component({
selector: 'Login',
providers: [
UserService,
LinkedinService
],
template: require('./login.html')
})
export class LoginComponent {
me: User;
constructor(public linkedinService: LinkedinService, public userService: UserService) {
this.me = userService.me;
}
ngOnInit() {
console.log('hello `Login` component');
}
login() {
this.linkedinService.login()
.then(() => this.linkedinService.getMe()
.then(profile => this.userService.setUser({ name: profile.firstName })));
}
}
linkedin.service.ts
import {Injectable} from 'angular2/core';
export interface User {
name: string;
email?: string;
picture?: string;
}
@Injectable()
export class UserService {
me: User;
constructor() {
}
setUser(user: User) {
this.me = user;
}
}
import {Injectable} from 'angular2/core';
declare const IN: any;
@Injectable()
export class LinkedinService {
constructor() {
IN.init({
api_key: 'xxxxxxxxxxx',
authorize: true
});
}
login() {
return new Promise((resolve, reject) => {
IN.User.authorize(() => resolve());
});
}
getMe() {
return new Promise((resolve, reject) => {
IN.API.Profile('me').result((profile) => resolve(profile.values[0]));
});
}
}
我试图从UserService导入用户界面并在LoginComponent中使用,但我不知道我做错了什么。有什么想法吗?我不确定是否必须使用LoginComponent内部的用户界面,对吗?请仔细了解代码:
.then(() => this.linkedinService.getMe())
.then(profile => this.userService.setUser({ name: profile.firstName })));
配置文件的类型由this.linkedinService.getMe()
的响应驱动。这似乎有点像承诺。它没有成员名firstName
。因此出现了错误:
Property 'firstName' does not exist on type '{}'.
修理
检查linkedinService
的代码/签名。这与问题包含的user.service.ts
文件无关,我检查了它,但无法解决。我还用user.service.ts
文件更新了我的问题。注意:如果这是你真正的api\u密钥,你应该删除它。其次,在大箭头语句中,执行console.log(profile)
,这样可以确保firstName
是返回对象的属性。您可以通过在语句中添加花括号来编写多个语句:.then(profile=>{console.log(profile);this.userService.setUser({name:profile.firstName});})代码>