Home > Web Front-end > Vue.js > What should I do if the vue menu does not refresh?

What should I do if the vue menu does not refresh?

藏色散人
Release: 2022-12-26 16:36:42
Original
2780 people have browsed it

Solution to vue menu not refreshing: 1. Write "" on the index page; 2. Set the cache name to "test-keep-alive"; 3. Configure "{path: '/Material',name: 'Material'...}" in the router file.

What should I do if the vue menu does not refresh?

#The operating environment of this tutorial: Windows 10 system, Vue version 3, Dell G3 computer.

What should I do if the vue menu does not refresh?

vue implements component switching tab (menu) page without refreshing the page

vue implements component switching tab (menu) page without refreshing the page (keep alive)

is a built-in component of Vue, which can retain the state in the memory during component switching to prevent repeated rendering of the DOM.

Official website explanation

When wrapping dynamic components, inactive component instances will be cached instead of destroying them. Similar to , is an abstract component: it does not render a DOM element by itself, nor does it appear in the parent component chain. When a component is switched within , its two life cycle hook functions, activated and deactivated, will be executed accordingly. In 2.2.0 and higher, activated and deactivated will fire on all nested components within the tree. Mainly used to preserve component state or avoid re-rendering.

Application Scenario

For example, when building a system with the function of switching components, labels are usually placed on the index page, plus the parent-child configuration of the router file. Component relationships and routing jumps enable component rendering of pages. But the effect of this is that every time the component is clicked, the page will be re-rendered, and the data on the page will not be retained. Therefore, using the tag wrapped with the tag can cache inactive components and still have the original information after returning.

Specific code

Write the following code on the index page

// 需要缓存的组件
<keep-alive v-if="showView">
    <router-view v-if="$route.meta.keepAlive"></router-view>
</keep-alive>
// 不需要缓存的组件
<router-view v-if="!$route.meta.keepAlive"></router-view>
Copy after login

The cache component name is the component test-keep-alive, and the include here is cache. , exclude is not cached

// 将缓存name为test-keep-alive的组件
<keep-alive include="test-keep-alive">
  <component></component>
</keep-alive>
// 将缓存name为teat,teat2的组件
<keep-alive include="teat,teat2">
  <component></component>
</keep-alive>
Copy after login

Configure the following code in the router file

// 需要缓存的组件
{
    path: &#39;/Material&#39;,
    name: &#39;Material&#39;,
    aliasName: &#39;物料信息&#39;,
    meta:{keepAlive: true}, // 是否缓存组件
    component: () => import(&#39;../components/Material/index.vue&#39;),
},
{
    path: &#39;/Unit&#39;,
    name: &#39;Unit&#39;,
    aliasName: &#39;单位信息&#39;,
    component: () => import(&#39;../components/Unit/index.vue&#39;),
}
Copy after login

Recommended learning: "vue.js Video Tutorial"

The above is the detailed content of What should I do if the vue menu does not refresh?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template