Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular2全局变量服务_Angular_Typescript_Angular Services - Fatal编程技术网

Angular2全局变量服务

Angular2全局变量服务,angular,typescript,angular-services,Angular,Typescript,Angular Services,我正在尝试创建一个全局变量来处理何时在我的网站上显示加载程序。我遵循了,但我得到了以下错误 泛型类型“Observer”要求此行有1个类型参数 this.ShowLoaderChange = new Observable((observer:Observer){ 此外,我似乎看不到他们在哪里为ChangeObserver声明了变量,因为我在这里使用: this.ShowLoaderChangeObserver = observer; 这里呢 this.ShowLoaderChangeObse

我正在尝试创建一个全局变量来处理何时在我的网站上显示加载程序。我遵循了,但我得到了以下错误

泛型类型“Observer”要求此行有1个类型参数

this.ShowLoaderChange = new Observable((observer:Observer){
此外,我似乎看不到他们在哪里为ChangeObserver声明了变量,因为我在这里使用:

this.ShowLoaderChangeObserver = observer;
这里呢

this.ShowLoaderChangeObserver.next(this.ShowLoader);


import { Observer } from 'rxjs/Observer';
import { Observable } from 'rxjs/Observable';
import { Injectable } from 'angular2/core';
import { Subject }    from 'rxjs/Subject';

@Injectable()
export class LoaderService {
  public ShowLoader;
  ShowLoaderChange: Observable<any>;

  constructor() {
     this.ShowLoaderChange = new Observable((observer:Observer){
         this.ShowLoaderChangeObserver = observer;
     })
  }

  setData() {
    this.ShowLoader = !this.ShowLoader;
    this.ShowLoaderChangeObserver.next(this.ShowLoader);
  }
}
我这样调用函数

import {LoaderService} from '../LoaderService'
export class AdminDashboardComponent implements OnInit{
constructor (private loader:LoaderService){}

ngOnInit():any{
   this.loader.setData();
} }

编辑:新服务


}

您需要指定可观察对象将产生的值的类型作为类型参数。在本例中,由于您已将observable键入ShowLoaderChange:observable,因此您也可以将observable的类型指定为任意类型:

this.ShowLoaderChange = new Observable((observer:Observer<any>) => {
    this.ShowLoaderChangeObserver = observer;
});

谢谢,这对我来说更有意义。唯一的问题是,我现在收到一个错误TypeError:无法读取this.ShowLoaderChangeObserver.nextthis.ShowLoader行[null]中未定义的属性'next';您如何调用setData?这是否指向setData内的LoaderService实例,并且位于this.ShowLoaderChangeObserver=observer;?我无法复制这个。您能在setData和observer:observer=>{this.ShowLoaderChangeObserver=observer;}中看到这方面的内容吗?然后,我在我的组件子级中调用service.setData。我也启动了我的LoaderService
import { Observer } from 'rxjs/Observer';
import { Observable } from 'rxjs/Observable';
import { Injectable } from 'angular2/core';
import { Subject }    from 'rxjs/Subject';

@Injectable()
export class LoaderService {
   public ShowLoader: boolean;
   ShowLoaderChange: Observable<boolean>;
   ShowLoaderChangeObserver: Observer<boolean>;

   constructor() {
      this.ShowLoaderChange = new Observable((observer:Observer<boolean>) 
     => {
         this.ShowLoaderChangeObserver = observer;
        })
   }

  setData() {
      this.ShowLoader = !this.ShowLoader;
      this.ShowLoaderChangeObserver.next(this.ShowLoader);
  }
this.ShowLoaderChange = new Observable((observer:Observer<any>) => {
    this.ShowLoaderChangeObserver = observer;
});
export class LoaderService {
  public ShowLoader: boolean;
  ShowLoaderChange: Observable<boolean>;
  ShowLoaderChangeObserver: Observer<boolean>;

  constructor() {
     this.ShowLoaderChange = new Observable((observer:Observer<boolean>) => {
         this.ShowLoaderChangeObserver = observer;
     })
  }

  setData() {
    this.ShowLoader = !this.ShowLoader;
    this.ShowLoaderChangeObserver.next(this.ShowLoader);
  }
}