【发布时间】:2021-07-09 03:19:32
【问题描述】:
早安,
搜索此错误并发现一些类似情况后,我无法将答案与我的问题(新手错误)联系起来。
当我尝试在我的 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();
感谢您的帮助。
【问题讨论】:
标签: angular typescript