First, describe
ANGULAR4 There is a very important method, called description (subscribe); First look at an example:
Getstudytime (userId) {
this.trainService.findStudyTime (userId)
. Subscribe (result=>{
This.trainlist=result
})
}
Here, we define a method: Getstudytime (), is to acquire learning time;
In normal terms, this.trainService.findStudyTime (userId), but why use a subscribe () method. Whether it is superfluous. Second, the source code
/** * Registers handlers for handling emitted values, error and completions from the observable, and * exec Utes the observable ' s subscriber function, which'll take action to set up the underlying data stream * @method Subs Cribe * @param {partialobserver| Function} observerornext (optional) either a observer defining all functions to be called, * or the The
Possible handlers, which is the handler for each value emitted from the observable. * @param {Function} error (optional) a handler for a terminal event resulting the from error. If no error handler is provided, * the error would 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;
Iii. Translation
The registration handler handles the values, errors, and finishes sent from the observable, and performs the description function of the observable, which can set the underlying data flow;
@method Subscribe method Name: Subscribe;
@param {Local Observer | function} observerornext (operation) or an observer (Observer) defines the functions of all calls;
What is an observer. The foreground method must carry the parameter, passes through the foreground, passes through an observer's observatory, crosses this layer observation deck, can arrive backstage, according to @get ("/api/v1/train/gettime") road origin to find the corresponding method; this barrier, is the observatory's Observatory (which has observer observer), and the observer calls the observation method:observable<any> to invoke, monitor methods and data; Here are three processors:
Error Processor : If there is an error, and there is no fault (error) processor to handle the error, then this is to ensure that the result of the final time to return the correct;
complete Complete the processor : If the final result is successfully completed;
Subscription Description Processor : Returns a description obtained according to the registered processor; Iv. format Introduction:
①subscribe (): Subscription; indicates subscribe (), the method return value belongs to the Subscribe class;
②subscribe (observer:partialobserver): Subscription; Incoming parameter is observer, the type of observer is: partialobserver generic ; subscrible is the subscription type;
③subscribe (Next: (value:t) => void, error?: (Error:any) => void, complete?: () => void): Subscription; incoming value is next , whether there is a next value, if there is a value, and the value is T, does not return; if it is an error state (error), it does not return; if it is a completion state (complete), it does not return;
④protected _trysubscribe (sink:subscriber): teardownlogic; this does not know much;
Back to our example:
Getstudytime (userId) {
this.trainService.findStudyTime (userId)
. Subscribe (result=>{
This.trainlist=result;
Loading.dismiss ();
})
}
If the return value result is normal and has a value, go to the next method:
This.trainlist=result;
Assigning the result to this.trainlist;
Here is a Watchtower method, this is my own analogy, from the front to the background through the fort, used to observe the data, the method is normal; lose the siege first: see if the person who comes in or out is: Zombie: error; normal person: complete; to be determined: subscribe;
Watchtower generally in the service.ts file inside; The service layer is usually the gas bag, both sides are bullied, such as the backstage service, above is the controller layer, the Congtroller layer is the foreground data entrance, is also the backstage data exportation; The service is at the bottom of the mapper layer, and the following connection is Database, which is responsible for passing data from database queries;
/*warn Acquisition Learning Time * *
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;
}
v. Summary
To sum up, subscribe is still very good, can cooperate with watchtower processing data, watchtower of the data from the background classification (3 categories), and then to subscribe to deal with, a bit like the Try-catch in Java
; it is still useful;