Angular2,EventEmitter上的订阅属性不存在? [英] Angular2, subscribe property on EventEmitter not existing?

查看:235
本文介绍了Angular2,EventEmitter上的订阅属性不存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的app.component包含网站的<nav>,该网站使用[routerLink]转到不同的页面.它导入每个页面的组件.页面之一是登录页面.如果用户尚未登录,我希望导航显示"Login"(登录)-如果用户为"Logout"(退出).

My app.component contains the <nav> of the website, which uses [routerLink] to go to the different pages. It imports each of the pages' components. One of the pages is a login page. If the user is not logged in, I want the navigation to say Login- if the user is, Logout.

app.component中:

my_userO : userO = undefined;

constructor (private my_userS:userS){
    my_userS.userChanged.suscribe(temp => this.updateNav());

updateNav(){ 
    this.my_userO = this.my_userS.getUser(); 
}

logOut(){
    this.my_userS.setUser(undefined);
}

及其模板中:

<li><a *ngIf="!my_userO" [routerLink]="['LoginPage']">Login</a></li>
<li><a *ngIf="my_userO" [routerLink]="['HomePage']"(click)="logOut()">Logout</a></li>

userS是全局服务(它在main.ts中引导,并且没有作为提供程序添加到我正在使用的任何组件中),我看起来像这样:

userS is a global service (it is bootstrapped in main.ts and not added as a provider in any of the components I am using it in) I have that looks like this:

public userChanged: EventEmitter<{}>
currentUser : userO = undefined;

constructor(private http:Http){
    this.userChanged = new EventEmitter();
}

getLogin(username: string, password: string): Promise<userO>{
    return this.http.get(this.getLoginUrl+username).toPromise().then(response => response.json().data).catch(this.handleError);
}

getUser(){
    return this.currentUser;
}

setUser(x_userO: userO){
    this.currentUser = x_userO;
    this.userChanged.emit(undefined);
}

在登录页面中,我只运行getLogin然后运行setUser,返回前者返回的内容.

In the login page, I just run getLogin then setUser with what the former returned.

我的问题是我收到错误消息:类型为EventEmitter< {}>的属性suscribe不存在".

My problem is that I get the error "Property suscribe does not exist on type EventEmitter<{}>".

推荐答案

是错字

使用subscribe,而不是suscribe

我没有足够的声誉来回答下面的哥伦布",但这是完全错误的.

I don't have enough reputation to answer Colum below but that's totally incorrect.

  1. emit()返回void
  2. EventEmitter扩展(继承)Subject,这是一个可观察的对象,也是一个观察者.

  1. emit() returns void
  2. EventEmitter extends (inherits) Subject which is an observable and an observer.

export declare class EventEmitter<T> extends Subject<T>

一个EventEmitter是一个经过一些修改的rxjs主题,最值得注意的是,它不使用next()向下游发送值,而是使用emit(). 它还允许在发出将是异步的地方设置异步逻辑.

an EventEmitter is an rxjs Subject with some modifications, most notably it does not use next() to send a value down the stream but uses emit(). It also allows setting an async logic where the emit will be async.

这篇关于Angular2,EventEmitter上的订阅属性不存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆