如何解决此表达式不可调用,“UserService”类型没有调用签名
美好的一天,
在搜索此错误并找到一些类似的情况后,我无法将答案与我的问题联系起来(新手错误)。
当我尝试在我的 add-new-message 组件中使用位于 user.service.ts
中的函数时,我遇到了标题中描述的错误
所以这是user.service.ts
import {Injectable} from '@angular/core';
import {HttpClient,HttpHeaders} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {GLOBAL} from './global';
import {User} from '../models/user';
@Injectable()
export class UserService{
public url:String;
public identity;
public token;
public stats;
public user;
constructor(public _http: HttpClient){
this.url =GLOBAL.url;
}
getIdentity(){
let identity = JSON.parse(localStorage.getItem('identity'));
if(identity != 'undefined'){
this.identity = identity;
}else{
this.identity= null;
}
return this.identity;
}
getToken(){
let token = localStorage.getItem('token');
if(token != "undefined"){
this.token = token;
}else {
this.token = null;
}
return this.token;
}
这是我的add.component.ts
import {Component,OnInit,DoCheck} from '@angular/core';
import {Router,ActivatedRoute,Params} from '@angular/router';
import {Follow} from '../../../models/follow';
import {Message} from '../../../models/message';
import {MessageService} from '../../../services/message.service';
import {FollowService} from '../../../services/follow.service';
import {User} from '../../../models/user';
import {UserService} from '../../../services/user.service';
import {GLOBAL} from '../../../services/global';
@Component({
selector: 'add',templateUrl: './add.component.html',providers: [
FollowService,MessageService,UserService
]
})
export class AddComponent implements OnInit {
public title: string;
public message: Message;
public identity;
public token;
public url: string;
public status: string;
public follows;
constructor(
private _route: ActivatedRoute,private _router: Router,private _followService: FollowService,private _messageService: MessageService,private _userService: UserService
){
this.title = 'New message';
this.message = new Message('','',this.identity._id,'');
this.identity = this._userService().getIdentity();
this.token = this._userService().getToken();
this.url = GLOBAL.url;
}
ngOnInit(){
console.log('add.component loaded');
}
}
所以 this.identity = this._userService().getIdentity();
和 this.token = this._userService().getToken();
都向我抛出同样的错误
This expression is not callable.
Type 'UserService' has no call signatures.
41 this.identity = this._userService().getIdentity();
感谢您的帮助。
解决方法
你不需要将_userService作为函数/方法调用,改成这样
this.identity = this._userService.getIdentity();
this.token = this._userService.getToken();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。