BehaviorSubject Observable在Angular 2 RC4中不起作用

时间:2016-09-02 23:02:30

标签: angular observable behaviorsubject

我仍在使用Angular 2 RC4,因为我有其他RC5问题并让HTTP在那里工作。在这种情况下,我试图使用BehaviorSubject observable来更新我在app.component.html中使用的布尔变量  如果用户成功登录,则仅显示数据(使用* ngIf)。我的应用程序在我在Eclipse Neon中部署的Tomcat 8服务器上运行良好。我认为我已经正确地遵循了其他示例,但我一直在Chrome中遇到此错误 - TypeError: Cannot read property 'next' of undefined

你能帮助澄清一下我缺少的东西吗?

auth.service.ts

import { Injectable }     from '@angular/core';
import { Headers, Http, Response, RequestOptions } from '@angular/http';
import { Observable, BehaviorSubject }     from 'rxjs/Rx';

@Injectable()
export class AuthService {

  public loggedIn:BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);

  afterLogin() {
     this.loggedIn.next(true);
  }

}

app.component.ts

import { Component }          from '@angular/core';
import { ROUTER_DIRECTIVES, Router }  from '@angular/router';
import { AuthService }        from './auth/auth.service';
import { Observable, Observer, Subscriber }     from 'rxjs/Rx';

@Component({
  moduleId: module.id,
  selector: 'my-app',
  templateUrl: 'app.component.html',
  directives: [ROUTER_DIRECTIVES],
  providers: [ AuthService ] 
})
export class AppComponent {

     public userLoggedOn: boolean;


     constructor (private _beginAuth: AuthService,
                  private router: Router) {}

     ngOnInit() { 

          this._beginAuth.loggedIn.subscribe(loggedIn => { this.userLoggedOn = loggedIn });

     }
}

main.ts

import {bootstrap}    from '@angular/platform-browser-dynamic';
import { Title } from '@angular/platform-browser';
import {AppComponent} from './app.component';
import {AuthService} from './auth/auth.service';
import {APP_ROUTER_PROVIDERS} from './app.routes';
import {HTTP_PROVIDERS} from '@angular/http';
import { disableDeprecatedForms, provideForms } from '@angular/forms';
//import {enableProdMode} from '@angular/core';


//enableProdMode();
bootstrap(AppComponent, [
  AuthService,
  APP_ROUTER_PROVIDERS,
  HTTP_PROVIDERS,
  Title,
  disableDeprecatedForms(),
  provideForms()
])
.catch((err: any) => console.error(err));

Chrome控制台出错:

app-bundle.min.js:1 TypeError: Cannot read property 'next' of undefined
    at t.e.extractData [as project] (http://localhost/labtool/app/app-bundle.min.js:1:27030)
    at t._next (http://localhost/labtool/app/app-bundle.min.js:52:16718)
    at t.next (http://localhost/labtool/app/app-bundle.min.js:47:21545)
    at XMLHttpRequest.p (http://localhost/labtool/app/app-bundle.min.js:34:28779)
    at ZoneDelegate.invokeTask (http://localhost/labtool/resources/js/zone.js/dist/zone.js:365:38)
    at Object.onInvokeTask (http://localhost/labtool/app/app-bundle.min.js:32:21063)
    at ZoneDelegate.invokeTask (http://localhost/labtool/resources/js/zone.js/dist/zone.js:364:43)
    at Zone.runTask (http://localhost/labtool/resources/js/zone.js/dist/zone.js:265:48)
    at XMLHttpRequest.ZoneTask.invoke (http://localhost/labtool/resources/js/zone.js/dist/zone.js:433:34)

可能错误是我正在使用的HTML语法。

<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li *ngIf="userLoggedOn" class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Admin Tools<span class="caret"></span></a>
          <ul class="dropdown-menu">
                   <li><a [routerLink]="['home']">Home</a></li>
          </ul>
        </li>
      </ul>
  </div>

1 个答案:

答案 0 :(得分:0)

更新 - 测试代码

我根据您的代码创建了一个测试基础,但我没有看到错误。

package.json依赖项

"dependencies": {
    "@angular/common": "2.0.0-rc.4",
    "@angular/compiler": "2.0.0-rc.4",
    "@angular/core": "2.0.0-rc.4",
    "@angular/forms": "0.2.0",
    "@angular/http": "2.0.0-rc.4",
    "@angular/platform-browser": "2.0.0-rc.4",
    "@angular/platform-browser-dynamic": "2.0.0-rc.4",
    "@angular/router": "3.0.0-beta.1",
    "es6-shim": "^0.35.0",
    "reflect-metadata": "0.1.3",
    "rxjs": "5.0.0-beta.6",
    "zone.js": "^0.6.12"
}

<强> app.component.ts

import {Component, OnInit} from '@angular/core';

import {AuthService} from './auth.service';

@Component({
    selector: 'app-component',
    template: `<h1>{{title}}</h1>`,
    providers: [AuthService]
})
export class AppComponent implements OnInit {
    title = 'SO-39301683';

    public userLoggedOn: boolean;

    constructor(private _beginAuth: AuthService) { }

    ngOnInit() {

        this._beginAuth
            .loggedIn
            .subscribe(loggedIn => {
                this.userLoggedOn = loggedIn;
            });

        // Manually trigger afterLogin()
        this._beginAuth.afterLogin();

    }
}

<强> auth.service.ts

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/Rx';

@Injectable()
export class AuthService {

  public loggedIn: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);

  afterLogin() {
    this.loggedIn.next(true);

    console.log('afterLogin() triggered.');
  }

}

<强> main.ts

// Polyfills
import 'es6-shim';
import 'reflect-metadata';
import 'zone.js/dist/zone';
import 'ts-helpers';

// Angular 2
import '@angular/platform-browser';
import '@angular/platform-browser-dynamic';
import '@angular/core';
import '@angular/common';
import '@angular/http';
import '@angular/router';

// RxJS
// import 'rxjs';

import { enableProdMode } from '@angular/core';
import { bootstrap } from '@angular/platform-browser-dynamic';
import { HTTP_PROVIDERS } from '@angular/http';

import { AppComponent } from './app/app.component';

// const ENV_PROVIDERS = [];
enableProdMode();

bootstrap(AppComponent, [HTTP_PROVIDERS])
    .catch(err => console.error(err));