微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

此表达式不可调用,“UserService”类型没有调用签名

如何解决此表达式不可调用,“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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?