How to use vue keep-alive to request data
This time I will show you how to use vue keep-alive to request data, and what are the precautions for using vue keep-alive to request data. The following is a practical case, let's take a look.
index page: home page brand entrance
list page: product list page
product page: product details page
When entering the list from the index page Refresh the page. There is no need to refresh the page when returning the list from the product page, so the list uses the keep-alive attribute and keepAlive is set to true. However, a problem was discovered during the development process. When returning from the product page to the list, the list page is not correct. brand list instead of the brand list from a previous click. Since everyone encounters different problems regarding incorrect keep-alive request data, here is my solution. Hope this can give you an idea.
Solution
Many people will change keep-alive by modifying keepAlive. After I tried it, it still didn’t work, so I changed my mind.
Execution sequence of hook functions
Do not use keep-alive
beforeRouteEnter --> created --> mounted --> ; destroyed
Use keep-alive
beforeRouteEnter --> created --> mounted --> activated --> deactivated
Literacy first, many people and I don’t know the above knowledge. In the keep-alive page, you can get the parameters of this.$route.params in activated
Avoid setting keepAlive. When the product returns, the data is incorrect. When the page enters the list, it is in a cached state. Then it is judged whether to execute the function in activated by whether it is entered from the index.
list.vue
beforeRouteEnter(to, from, next) { //判断从index页面进入,将list的isBack设置为true //这样就可以请求数据了 if (from.name == 'index') { to.meta.isBack = true; } next(); }, activated: function () { if (this.$route.meta.isBack || this.isFirstEnter) { //清理已有商品列表的数据,重新请求数据,如果不清除的话就会有之前的商品缓存,延迟显示最新的商品 this.proData = []; //请求数据 this.fetchData(); } //重新设置当前路由的isBack this.$route.meta.isBack = false; //重新设置是否第一次进入 this.isFirstEnter = false; }, mounted: function () { //如果是第一次进入,或者刷新操作的话,也请求数据 this.isFirstEnter = true; },
router.js
const appRouter = { mode: "history", base: "/m/", routes: [ { path: "", redirect: "/index" }, { path: "/index", name: "index", component: Index, meta: { keepAlive: true } }, { path: "/list", name: "list", component: List, meta: { keepAlive: true, isBack: false //isback是true的时候请求数据,或者第一次进入的时候请求数据 } }, { path: "/product/:id", name: "product", component: Product, meta: { keepAlive: false } } ] }; Vue.use(Router); export default new Router(appRouter);
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to use seajs Write convention in require
The above is the detailed content of How to use vue keep-alive to request data. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

There are three ways to refer to JS files in Vue.js: directly specify the path using the <script> tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses <router-link to="/" component window.history.back(), and the method selection depends on the scene.

There are three common methods for Vue.js to traverse arrays and objects: the v-for directive is used to traverse each element and render templates; the v-bind directive can be used with v-for to dynamically set attribute values for each element; and the .map method can convert array elements into new arrays.

There are two ways to jump div elements in Vue: use Vue Router and add router-link component. Add the @click event listener and call this.$router.push() method to jump.
