本文整理匯總了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));
}