简体   繁体   English

未在Observable.create对象上触发订阅

[英]Subscribe is not being triggered on Observable.create object

So I have the following: 所以我有以下几点:

const getAllData = Observable.create( () => {
    console.log(Date.now() + ' - Calling getallcontenttypes');
    this.getAllContentTypes();
    console.log(Date.now() + ' - Calling getalltaxonomysitecolumns');
    this.getAllTaxonomySiteColumns();
  });
  getAllData.subscribe( () => {
    console.log(Date.now() + ' - Subscribed, data loaded, calling router...');
    this.router.navigateByUrl('/contenttype');
  });  

The functions inside my const getAllData work, get the data and handle it (with additional console logs so I know there's values and they run through those functions). 我的const getAllData内部的函数可以工作,获取数据并进行处理(带有其他控制台日志,因此我知道有值,它们通过这些函数运行)。

What I want to do now is, once all my functions (for now 2) inside my getAllData are finished getting & handling all the data, I want to reroute to another view. 我现在想做的是,一旦我在getAllData中的所有函数(现在是2)完成了对所有数据的获取和处理,我想重新路由到另一个视图。 Those 2 functions (getall...) return void now. 那两个函数(getall ...)现在返回void。 This is all inside my ngOnInit(). 这些都在我的ngOnInit()中。

But so far it seems like the .subscribe() doesn't trigger or maybe I'm doing it wrong. 但是到目前为止,似乎.subscribe()不会触发,或者我做错了。 Can anyone see what's the issue? 谁能看到问题所在?

For example, here's the getAllContentTypes function: 例如,这是getAllContentTypes函数:

public getAllContentTypes() {

const jsonStringified = JSON.stringify(json);

this.contentTypeService.getAllContentTypes(jsonStringified).pipe(
  operators.tap(res => this.convertJsonResultToArrayCT(res)))
  .subscribe(res2 => this.storeInSessionStorage(res2, 'ContentTypes'));
}  

public storeInSessionStorage(res: any, key: string) {
    sessionStorage.setItem(key, JSON.stringify(this.contentTypeArray));
    console.log(Date.now() + ' - Stored in sessionstorage: ' + key);
}  

And here's an image of what I get in my console: https://imgur.com/a/xQhaqjv . 这是我在控制台中得到的图像: https : //imgur.com/a/xQhaqjv

EDIT: 编辑:

OK So I've modified my functions to return Observables: 确定,所以我修改了函数以返回Observables:

public getAllContentTypes(): Observable<ContentType> {

const jsonStringified = JSON.stringify(json);

this.contentTypeService.getAllContentTypes(jsonStringified).pipe(
  operators.tap(res => this.convertJsonResultToArrayCT(res)))
  .subscribe(res2 => this.storeInSessionStorage(res2, 'ContentTypes'));

return from(this.contentTypeArray);

} }

I've also edited my code as such: 我还对代码进行了如下修改:

const test = forkJoin(
    this.getAllContentTypes(),
    this.getAllTaxonomySiteColumns()
  ).subscribe( () => {
    console.log(Date.now() + ' - Subscribed, data loaded, calling router...');
    this.router.navigateByUrl('/contenttype');
  });  

But subscribe isn't triggered. 但是订阅不会触发。

When you create an observable with the method create(), you have to pass an observer as an argument so you can call next(), error() or complete() on it later, eg 当使用create()方法创建可观察对象时,必须将观察者作为参数传递,以便稍后可以在其上调用next(),error()或complete()。

const getAllData = Observable.create( observer => { 
                                     observer.next('123');
                                    });

When the code subscribes to this observable, it has to pass a function to handle the emitted value, and optionally error and completion, ie the observer eg 当代码订阅该可观察对象时,它必须传递一个函数来处理发出的值,并可选地处理错误和完成,即观察者,例如

getAllData.subscribe(value => console.log(value));  // prints 123

I have a working code sample here: https://codepen.io/yfain/pen/xLaMdN?editors=1012 我在这里有一个工作代码示例: https : //codepen.io/yfain/pen/xLaMdN?editors=1012

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM