How can the two components under the two routes of Vue use the data in the same Vuex without sending http requests repeatedly?

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.
clipboard.png

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:

  1. 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.
  2. my http request is made with vue-resource . After making a request, you can save the Promise returned by vue-resource as a variable, and change the variable to a false value after Resolved . When calling again, check whether promise 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 storing promise will be troublesome.

so ask if you have a better way, or a better business logic, to correct my mistakes.

Mar.23,2021

these two methods are not necessarily better

  1. if you don't access the components that need this data, will this data be superfluous, and what if your other pages have such logic? Is it just a lot of useless data.
  2. isn't that historical data all the time, if I really need new data right now?
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. How do I know that the data is being requested without having to make another request?

my personal view is that this is a pseudo-demand. First of all, this has no impact on the server, the browser has a limit on the number of homologous TCP, the same request also has cache locks, and secondly, the server itself generally does caching, so first of all, there will be no pressure on the server. For the client, how often does jump to the second page that requires the same data when opening the first page ? I don't think so often. What will be the impact after it happens? Maybe it's just a waste of traffic. So I don't think it's a problem, because if you want to solve this problem, it will lead to more problems.

secondly, if you really need it, you can access the same Promise without the need for true or false values of variables. Promise promises that the result will be certain and will not be changed. But you need to consider the issue of limitation and avoid accessing historical data all the time.


do a loading, before the data request is completed to keep the user from jumping. After completion = "have data =" can be redirected
http requests up to 3000ms

Menu