vue.js - 在路线更改时停止整页重新渲染?(Nuxt.js)
问题描述
我正在尝试使用_.vue在 Nuxt 中实现自定义路由。我的 _.vue 页面文件有两个子组件(我们称它们为 Category 和 Product),每个子组件都会在它们的数据存在于 store 中时使用 v-if 显示。我正在使用 _.vue 组件的中间件来处理自定义路由。
我的问题是我的所有组件都会在每次路由更改时重新渲染,这会导致延迟和图像闪烁。
让我解释一下我想要实现的目标。有一个类别中的产品列表。当您单击一个产品时,它会在一个模式窗口中打开,该类别仍在后台,并且还会更改当前页面的 URL,从而更改路由。当您关闭产品时,它应该回到与以前相同的状态。一切似乎都在按需要工作,除了当我打开或关闭产品时,我的所有组件组件都会重新渲染(包括 _.vue)。我尝试命名它们,使用 key() 等,但没有结果。
有没有办法让当前组件保持路由更改而不重新渲染?或者有解决方法吗?
<template>
<div>
<Category v-if="current_category" />
<Product v-if="current_product" />
</div>
</template>
<script>
import {mapState} from 'vuex'
import Product from '~/components/product/Product'
import Category from '~/components/category/Category'
export default {
middleware: 'router',
scrollToTop: false,
components: {
Product,
Category,
},
computed: {
...mapState({
current_category: state => state.current_category,
current_product: state => state.current_product,
}),
},
mounted: function() {
console.log('_ component mounted')
},
}
</script>
解决方案
您应该在页面组件中使用“key”选项。默认值为“route.fullPath”,因此您会在更改 URL 参数后看到重新渲染。 https://nuxtjs.org/docs/2.x/features/nuxt-components#the-nuxt-component
推荐阅读
- angular - Ionic4-angular 聊天机器人和对话流 v2
- discord.js - 如何使用命令解禁用户?
- sql - 在冲突中使用插入时 Postgres 语法错误
- google-chrome-extension - 有没有办法阻止导航?
- spring-boot - 尝试使用 SSL 将 Spring-Boot 应用程序连接到 Keycloak
- python - anaconda 无法识别 Hugginface 变压器模块
- c# - NavigationService.Navigate 异步行为
- html - 背景图像线性渐变在导航栏上不起作用
- javascript - 存在的数组中的对象返回未定义
- java - Spring MVC 从控制器获取日期到 JSP