【发布时间】:2019-02-24 06:19:35
【问题描述】:
我想创建一个可以在 Angular 6 中传递一些参数的 observale。 下面是在 Angular 站点 https://angular.io/guide/observables 中创建 observable 的示例代码,但它没有解释如何传递任何参数。
// Create an Observable that will start listening to geolocation updates
// when a consumer subscribes.
const locations = new Observable((observer) => {
// Get the next and error callbacks. These will be passed in when
// the consumer subscribes.
const {next, error} = observer;
let watchId;
// Simple geolocation API check provides values to publish
if ('geolocation' in navigator) {
watchId = navigator.geolocation.watchPosition(next, error);
} else {
error('Geolocation not available');
}
// When the consumer unsubscribes, clean up data ready for next subscription.
return {unsubscribe() { navigator.geolocation.clearWatch(watchId); }};
});
// Call subscribe() to start listening for updates.
const locationsSubscription = locations.subscribe({
next(position) { console.log('Current Position: ', position); },
error(msg) { console.log('Error Getting Location: ', msg); }
});
// Stop listening for location after 10 seconds
setTimeout(() => { locationsSubscription.unsubscribe(); }, 10000);
我想要的是在订阅时传递一些参数给 observable,我猜 observable 的定义可能是这样的:
const myobservable(a, b) = new Observable((observer) => {
...
})
您能告诉我该怎么做吗?
【问题讨论】:
标签: angular observable