本文整理汇总了TypeScript中rxjs.Observable.subscribe方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Observable.subscribe方法的具体用法?TypeScript Observable.subscribe怎么用?TypeScript Observable.subscribe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rxjs.Observable
的用法示例。
在下文中一共展示了Observable.subscribe方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: ngOnInit
ngOnInit() {
this.appMode$.subscribe((mode: AppModes) => {
this.show = mode !== AppModes.Add && mode !== AppModes.Edit;
});
}
示例2: subscribeToSaveResponse
private subscribeToSaveResponse(result: Observable<HttpResponse<IBankAccount>>) {
result.subscribe((res: HttpResponse<IBankAccount>) => this.onSaveSuccess(), (res: HttpErrorResponse) => this.onSaveError());
}
示例3: expect
expect(() => {
window.subscribe();
}).to.throw(ObjectUnsubscribedError);
示例4: setTimeout
setTimeout(() => {
sequence.subscribe({
next(num) { console.log('2nd subscribe: ' + num); },
complete() { console.log('2nd sequence finished.'); }
});
}, 500);
示例5: filter
export const findCodeViews = (codeHost: CodeHost, watchChildrenModifications = true) => (
containers: Observable<Element>
) => {
const codeViewsFromList: Observable<ResolvedCodeView> = containers.pipe(
filter(() => !!codeHost.codeViews),
mergeMap(container =>
from(codeHost.codeViews!).pipe(
map(({ selector, ...info }) => ({
info,
matches: container.querySelectorAll<HTMLElement>(selector),
}))
)
),
mergeMap(({ info, matches }) =>
of(...matches).pipe(
map(codeView => ({
...info,
codeView,
}))
)
)
)
const codeViewsFromResolver: Observable<ResolvedCodeView> = containers.pipe(
filter(() => !!codeHost.codeViewResolver),
map(container => ({
resolveCodeView: codeHost.codeViewResolver!.resolveCodeView,
matches: container.querySelectorAll<HTMLElement>(codeHost.codeViewResolver!.selector),
})),
mergeMap(({ resolveCodeView, matches }) =>
of(...matches).pipe(
map(codeView => ({
resolved: resolveCodeView(codeView),
codeView,
})),
filter(propertyIsDefined('resolved')),
map(({ resolved, ...rest }) => ({
...resolved,
...rest,
}))
)
)
)
const obs = [codeViewsFromList, codeViewsFromResolver]
if (watchChildrenModifications) {
const possibleLazilyLoadedContainers = new Subject<HTMLElement>()
const mutationObserver = new MutationObserver(mutations => {
for (const mutation of mutations) {
if (mutation.type === 'childList' && mutation.target instanceof HTMLElement) {
const { target } = mutation
possibleLazilyLoadedContainers.next(target)
}
}
})
containers.subscribe(container =>
mutationObserver.observe(container, {
childList: true,
subtree: true,
})
)
const lazilyLoadedCodeViews = possibleLazilyLoadedContainers.pipe(findCodeViews(codeHost, false))
obs.push(lazilyLoadedCodeViews)
}
return merge(...obs).pipe(
emitWhenIntersecting(250),
filter(({ codeView }) => !codeView.classList.contains('sg-mounted'))
)
}
示例6: ngOnInit
ngOnInit(): void {
this.counter$.subscribe((state: CounterModel): void => {
this.c$ = { ...state };
});
}
示例7: subscribeToSaveResponse
private subscribeToSaveResponse(result: Observable<Customer>) {
result.subscribe(() => this.onSaveSuccess(), () => this.onSaveError());
}
示例8:
* - the execution can be disposed.
*/
// how to create a new Observable:
//
// the constructor argument is a function that is called when the Observable is
// initially subscribed to.
//
// This function represents an Observable execution that happens for any
// Observer/Consumer that subscribes.
// The subscriber is an object that has 3 methods:
//
// - `next` can be called to emit new values that will be consumed.
// - `error` can be called to raise an error.
// - `complete` can be called to notify of a successful completion.
const o$ = new Observable<number>(subscriber => subscriber.next(1));
// creates a Subscription: an "execution environment" for the observer/consumer.
// the observable will start emitting values only after a subscription.
const subscription = o$.subscribe(v => console.log("subscriber: " + v));
console.log(`is the observable active? ${!subscription.closed}`);
// the subscription can be used to cancel the execution, more on this later on.
// Output:
//
// subscriber: 1
// is the observable active? true
示例9:
this.productSearchListUpdateService.change.subscribe((result: Observable<Object>) => {
this.searchTerm = this.productSearchListUpdateService.searchTerm;
result.subscribe(res => {
this.productList = res.results;
});
});
示例10:
/**
* Make Cold observable to Hot observable
*
* On caller function will not need to make ".subscribe(...)" to execute procedure
*
* @param cold Observable
*/
public static makeHot<T>(cold: Observable<T>) {
const subject = new Subject<T>();
cold.subscribe(subject);
return new Observable<T>((observer: any) => subject.subscribe(observer));
}