Data Fetching | vue router created
Sometimesyouneedtofetchdatafromtheserverwhenarouteisactivated.Forexample,beforerenderingauserprofile,youneedtofetchtheusersdatafromtheserver.Wecanachievethisintwodifferentways:FetchingAfterNavigation:performthenavigationfirst,andfetchdataintheincomingcomponentslifecyclehook.Displayaloadingstatewhiledataisbeingfetched.FetchingBeforeNavigation:Fetchdatabeforenavigationintherouteenterguard,andperformthenavigationafterdatahasbeenfetched.Technically,botharevalidchoices-itultimatelydependsontheuse...
Sometimes you need to fetch data from the server when a route is activated. For example, before rendering a user profile, you need to fetch the users data from the server. We can achieve this in two different ways:
Fetching After Navigation: perform the navigation first, and fetch data in the incoming components lifecycle hook. Display a loading state while data is being fetched.
Fetching Before Navigation: Fetch data before navigation in the route enter guard, and perform the navigation after data has been fetched.
Technically, both are valid choices - it ultimately depends on the user experience you are aiming for.
Fetching After Navigation [1]When using this approach, we navigate and render the incoming component immediately, and fetch data in the component itself. It gives us the opportunity to display a loading state while the data is being fetched over the network, and we can also handle loading differently for each view.
Lets assume we have a Po...