一、描述
Angular4裡面有一個非常重要的方法,叫描述(subscribe);首先看一個例子:
getStudyTime(userId){ this.trainService.findStudyTime(userId) .subscribe(result=>{ this.trainList=result; }) }
這裡,我們定義了一個方法:getStudyTime(),就是擷取學習時間;
按照正常來說,this.trainService.findStudyTime(userId);即可,但是為甚麼要用個subscribe()方法呢。是否是多此一舉。 二、原始碼
/** * Registers handlers for handling emitted values, error and completions from the observable, and * executes the observable's subscriber function, which will take action to set up the underlying data stream * @method subscribe * @param {PartialObserver|Function} observerOrNext (optional) either an observer defining all functions to be called, * or the first of three possible handlers, which is the handler for each value emitted from the observable. * @param {Function} error (optional) a handler for a terminal event resulting from an error. If no error handler is provided, * the error will be thrown as unhandled * @param {Function} complete (optional) a handler for a terminal event resulting from successful completion. * @return {ISubscription} a subscription reference to the registered handlers */ subscribe(): Subscription; subscribe(observer: PartialObserver<T>): Subscription; subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void): Subscription; protected _trySubscribe(sink: Subscriber<T>): TeardownLogic;三、翻譯
註冊處理常式處理從observable發送過來的值,錯誤和完成情況,並且執行observable的說明功能,這個功能可以設定基礎的資料流;
@method subscribe 方法名:subscribe;
@param {局部觀察者|功能} observerOrNext(操作)或者一個觀察者(observer )定義所有調用的功能;
什麼是觀察者。前台方法要帶著參數,穿過前台時,要穿過一個觀察者的瞭望台,穿過這層瞭望台,才能到達後台,根據@GET("/api/v1/train/getTime") 路由來找到對應的方法;這個壁壘,就是觀察者的瞭望台(裡面有觀察者observer);觀察者調用觀察方法:Observable<any>來調用,監控方法和資料;這裡有三個處理器:
error錯誤處理器:如果出現了錯誤(error),並且沒有錯誤(error)處理器來處理這個錯誤,那麼這個是為了保障最終時間的結果能夠正確的返回;
complete完成處理器:如果最終結果順利完成;
subscription描述處理器:返回一個根據註冊處理器而獲得的描述; 四、格式介紹:
①subscribe(): Subscription;表示subscribe(),方法傳回值是屬於Subscribe類;
②subscribe(observer: PartialObserver): Subscription;傳入參數為觀察者,觀察者的類型是:PartialObserver泛型;subscrible是Subscription類型的;
③subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void): Subscription;傳入值是next,進行判斷;是否有下一個值;如果有值,且值為T,則不返回;如果是報錯狀態(error),則不返回;如果是完成狀態(complete),也不返回;
④protected _trySubscribe(sink: Subscriber): TeardownLogic;這個不太瞭解;
回到我們的例子:
getStudyTime(userId){ this.trainService.findStudyTime(userId) .subscribe(result=>{ this.trainList=result; loading.dismiss(); }) }
如果傳回值result是正常的,有值的,那麼就進入下一個方法:
this.trainList=result;
把result賦值給this.trainList;
下面是一個瞭望塔方法,這是我自己打的比方,從前台到後台經過的堡壘,用來觀測資料,方法是否正常;就先喪失圍城:查看進來或出去的人是否是: 殭屍:error; 正常人:complete; 待定:subscribe;
瞭望塔一般在service.ts檔案裡面;服務層一般都是夾氣包,兩邊受氣;比如背景service,上面是controller層,congtroller層是前台資料的入口,也是後台資料的出口;service的底層是mapper層,下面串連的是資料庫,負責傳遞從資料庫查詢得來的資料;果然是服務行業;
/*warn 擷取學習時間*/ findStudyTime(userId:number):Observable<any>{ return this.getTimeStudy(userId) .map(response=>{ return response.data || []; }); } @GET("/api/v1/train/getTime") getTimeStudy(@Query('userId')userId):Observable<any>{ return null; }五、綜述
綜上所述,subscribe還是很好的,能夠配合瞭望塔處理資料;瞭望塔把後台傳來的資料進行分類(3類),然後交給subscribe來處理, 有點類似Java中的Try-catch
;還是很有用處的;