javascript - 在 Vue SPA 站点中重新加载同一页面
问题描述
我已经尝试了十几种方法来做到这一点。.htaccess
在 apache 托管(我的网站托管的首选环境)中,通过 heroku,通过带有_redirects
文件和netlify.toml
文件的 netlify。而且它们似乎都不起作用。Vue 推荐这样写.htaccess
文件:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
但它不会重新加载到同一页面。它重新加载到 index.html 主页面。现在,我明白为什么要这样做了。本质上,SPA 中的每个页面都是一个 index.html。正是这些组件使它看起来像一个不同的页面。但是,这适用于我见过的每个 SPA 网站。所以当我重新加载时,必须有一种方法让渲染保持一致。
如果它很重要,这里是一些相关的代码
环境
- VueJS 2
- 类星体 1.0
- 火力基地
- Vuew 路由器(在 VueJS 中开箱即用)
QUASAR.CONF.JS
build: { vueRouterMode: "history" }
ROUTES.JS(最小)
import Layout from "layouts/Layout.vue";
import Home from "pages/Index.vue";
import About from "pages/About.vue";
import Contact from "pages/Contact.vue";
const routes = [
{
path: "/",
component: Layout,
children: [
{ path: "/", component: Home },
{ path: "/about", component: About },
{ path: "/contact", component: Contact }
]
}
];
// Always leave this as last one
if (process.env.MODE !== "ssr") {
routes.push({
path: "*",
component: () => import("pages/Error404.vue")
});
}
export default routes;
如果你有什么需要看的,请告诉我。
问题页面加载正常,但是当我重新加载页面时,它会加载主页(主页),即索引页面。如果没有 htaccess 代码,它会显示找不到页面(甚至不是我自己的 404)。历史模式从 URL 中删除 # 并将其置于哈希模式也不能修复它。
任何帮助,将不胜感激
解决方案
通过在路由文件(我的设置中的 src/routes/index.js )中设置模式和路由器,我能够毫无问题地将我的应用程序从哈希转换为历史记录:
import Vue from 'vue';
import VueRouter from 'vue-router';
import Home from '../views/Home.vue';
import NotFound from '../components/NotFound.vue';
Vue.use(VueRouter);
const routes = [
{
path: '/',
name: 'home',
component: Home,
},
{
path: '*',
component: NotFound,
},
];
const router = new VueRouter({
mode: 'history',
routes,
});
export default router;
通过设置模式并导出路由器,我能够从我的应用程序中解决 404 问题。然而,Quasar 可能正在做其他事情。希望这种方法可以适合您。
推荐阅读
- javascript - Angular 5 ngModel 不更新值
- python - 对列表中的特定元素进行方程式,然后使用结果与下一个元素进行方程式?
- react-native - 排毒自动化框架指南:React Native?创建 End 2 端和集成测试框架
- c - 使用双指针的二叉树级顺序遍历
- javascript - 如何在Javascript中将数组值映射到对象内的数组?
- git - 当我认为应该更新时,更新 git 钩子没有回显?
- java - 将 YAML 文件中的列表映射到对象列表
- scala - 在哪里放置不可变函数定义?
- python-3.x - 如何从调度程序中获取错过的作业
- javascript - 如何在异步功能上使用 debounce?