當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript Observable.throw方法代碼示例

本文整理匯總了TypeScript中rxjs/Rx.Observable.throw方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Observable.throw方法的具體用法?TypeScript Observable.throw怎麽用?TypeScript Observable.throw使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在rxjs/Rx.Observable的用法示例。


在下文中一共展示了Observable.throw方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: constructor

    constructor(fb:FormBuilder) {
        this.formModel = fb.group({
            search: []
        });

        var searchControl = this.formModel.find('search');
        searchControl.valueChanges
            .debounceTime(400)
            .map(str => (str.replace(' ', '-')))
            .subscribe(x => console.log(x));


        /** travel search */
        var startDates = [];
        var startDate = new Date();

        for (var day = -2; day < 2; day++) {
            var date = new Date(
                startDate.getFullYear(),
                startDate.getMonth(),
                startDate.getDate() + day
            );
            startDates.push(date);
        }

        Observable
            .fromArray(startDates)
            .map(date => {
                console.log("Getting deals for date: " + date);
                return [1, 2, 3];
            })
            .subscribe(x => console.log(x));

        /** interval */
        // Observable
        //     .interval(1000)
        //     .subscribe(x => console.log(x))

        // Observable
        //     .interval(1000)
        //     .flatMap(x => {
        //         console.log("Calling server to get latest news..");
        //         return Observable.of([1, 2, 3]);
        //     })
        //     .subscribe(news => console.log(news));

        /** delay */
        var userStream = Observable.of({
            userId: 1, userName: 'chanu'
        }).delay(2000);

        /** join */
        var tweetsStream = Observable.of([1, 2, 3]).delay(1500);

        Observable
            .forkJoin(userStream, tweetsStream)
            .map(joined => Object({user: joined[0], tweets: joined[1]}))
            .subscribe(result => console.log(result));

        /** error handling */
        var observable = Observable.throw(new Error("Something failed!"));
        observable
            .subscribe(
                x => console.log(x),
                error => console.log(error));

        /** retry */
        var counter = 0;
        var ajaxCall = Observable
            .of('url')
            .flatMap(() => {
                if (++counter < 2)
                    return Observable.throw(new Error("Request failed!"));
                return Observable.of([1, 2, 3]);
            })
            .retry(3)
            .subscribe(
                x => console.log("Response from retry: " + x),
                error => console.log(error)
            );

        /** catching and continuing */
        var remoteDataStream = Observable.throw(new Error("Something is wrong!"));
        remoteDataStream.catch(err => {
                var localDataStream = Observable.of([1, 2, 3]);
                return localDataStream;
            })
            .subscribe(x => console.log("Catching and continuing: " + x));

        /** Completed */
        Observable
            .throw(new Error("error"))
            .subscribe(
                x => console.log(x),
                error => console.log(error),
                () => console.log("Completed"));

        Observable
            .fromArray([1, 2, 3])
            .subscribe(
//.........這裏部分代碼省略.........
開發者ID:chanusukarno,項目名稱:Angular2-Examples,代碼行數:101,代碼來源:observables.component.ts

示例2: handleError

 private handleError(error: any) {
     // console.log("Error", error);
     let errMsg = error.message || 'Server error';
     return Observable.throw(errMsg);
 }
開發者ID:enridaga,項目名稱:dinowolf,代碼行數:5,代碼來源:logs.service.ts

示例3: handleError

 private handleError (error: Response) {
     console.error(error);
     return Observable.throw(error.json().error || 'Server error');
 }
開發者ID:Montanasdelpablo,項目名稱:Moltin-Angular-App,代碼行數:4,代碼來源:data.service.ts

示例4: handleError

 private handleError(error: Response) {
     this.notificationsService.error(this.messagesService.oops, 
                  this.messagesService.somethingWentWrong);
                  
     return Observable.throw('Server error');
 }
開發者ID:redrick-tmn,項目名稱:SimpleAngularBlogApp,代碼行數:6,代碼來源:auth-http.service.ts

示例5: spyOn

 spyOn(instance.imagesService, 'getProfileImages').and.callFake(() => { return Observable.throw(new Error()); });
開發者ID:eljesaz,項目名稱:OPEN_Launcher,代碼行數:1,代碼來源:RegisterComponent.spec.ts

示例6:

 observable1 = constructorZone1.run(() => {
   return Rx.Observable.throw(error);
 });
開發者ID:jahtalab,項目名稱:zone.js,代碼行數:3,代碼來源:rxjs.throw.spec.ts

示例7:

 .catch(ex => {
     this._synchronizedExcecutionInfo.next(ExecutionInfo.createFail<TResult>());
     this._exceptions.next(ex);
     return Observable.throw(ex);
 })
開發者ID:KallynGowdy,項目名稱:RxUI,代碼行數:5,代碼來源:reactive-command.ts

示例8: _handleError

 private _handleError(error: Response) {
   return Rx.Observable.throw(error || 'Server error');
 }
開發者ID:prashmukho,項目名稱:Angular-2-Supers,代碼行數:3,代碼來源:heroes.service.ts

示例9: Error

 }).catch((error: any) => {
     // console.log(error.status, "service")
     if (error.status === 409) {
         return Observable.throw(new Error(error.status));
     }
 }
開發者ID:PardeepJain,項目名稱:Demo-Setup-,代碼行數:6,代碼來源:GlobalService.ts

示例10: handleError

 private handleError(error:any) {
   let errMsg = (error.message) ? error.message :
     error.status ? `${error.status} - ${error.statusText}` : 'Server error';
   return Observable.throw(errMsg);
 }
開發者ID:ravihamsa,項目名稱:angular2-starter-examples,代碼行數:5,代碼來源:project-list.ts


注:本文中的rxjs/Rx.Observable.throw方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。