my younger brother is not talented. I"m learning Vue
. I"m going to use Douban"s API to practice a handwritten single-page application
. I encountered a problem with http request
.
as shown in the figure:
I need to use the same movie list data in vuex
requested by Douban api in both the movie list component and the routing page showing the complete movie list.
I call action
when the component create ()
in the diagram to get the data. On the routing page showing the full movie list, I also intend to request it when the component create ()
of this data is needed. but which page routing component will be created first (if entered directly through the browser address) cannot be determined
. There is a problem with
.
when I open the first page, I quickly jump to the second page that needs the same data. At this time, the http request of the component in the first page has been made, but the data has not yet come back, and there is no data in vuex
. At this time, how do I know that the data is being requested without having to make another request
?
I have two ways to think of myself, but I want you to teach me a better one:
- put the action call of the http request in the create () of the parent component containing this < router-view >, so you don"t have to worry about whether the request has been made in the child component.
- my http request is made with
vue-resource
. After making a request, you can save thePromise
returned byvue-resource
as a variable, and change the variable to a false value afterResolved
. When calling again, check whetherpromise
is saved before deciding whether to request it again. But if there is a similar problem in other places, you have to write something similar. If you want to encapsulate, some requests will be sent with different parameters, so storingpromise
will be troublesome.
so ask if you have a better way, or a better business logic, to correct my mistakes.