【问题标题】:This expression is not callable, Type 'UserService' has no call signatures此表达式不可调用,类型“UserService”没有调用签名
【发布时间】: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


    【解决方案1】:

    你不需要将_userService作为函数/方法调用,改成这个

    this.identity = this._userService.getIdentity();
    this.token = this._userService.getToken();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      • 2020-08-08
      • 2020-12-27
      • 1970-01-01
      • 2020-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多