当前位置: 首页>>代码示例>>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;未经允许,请勿转载。