提问者:小点点

Vue路由器页面刷新或URL访问


我有一个登录页面与注册/登录从重定向到主页与内容。成功登录后,我重定向如下:

this.$router.push({ name: 'main' })

这是可行的,但如果我刷新页面或尝试从url访问它,例如http://testapp.test/main我得到错误:404页不存在。目前,我没有任何保护措施来防止访问仅针对登录用户的页面,我还为未定义的路由在路由器中添加了“*”路径,它也只是抛出404而不是加载主页。以下是我的路由器设置:

import Vue from 'vue'
import VueRouter from 'vue-router'
import BootstrapVue from 'bootstrap-vue'
import {store} from './store/store'

Vue.use(BootstrapVue);
Vue.use(VueRouter);

window.Vue = require('vue');


import Home from './components/LandingPage.vue'
import Main from './components/MainPage.vue'
import Logout from './components/Logout.vue'

const router = new VueRouter({
    mode: 'history',
    routes: [
        {
            path: '/',
            name: 'home',
            component: Home,
        },
        {
            path: '/main',
            name: 'main',
            component: Main,
        },
        {
            path: '/logout',
            name: 'logout',
            component: Logout,
        },
        {
            path :'*',
            name: 'home',
            component: Home,
        }
    ],
});


const app = new Vue({
    el: '#app',
    components: { Home, Main, Logout },
    router,
    store,
});

我试着https://router.vuejs.org/guide/essentials/history-mode.html#example-server-configurations,但我不确定我是否做得对。我所做的是复制apache配置的代码,并用它替换. htaccess中的现有代码。但是,即使从登录路由停止工作,如果我访问 /main它给我404错误。


共2个答案

匿名用户

我加了一句就解决了

Route::get('/{vue_capture?}', function () { return view('welcome'); })->where('vue_capture', '[\/\w\.-]*');

进入网络。php文件

匿名用户

您还必须为此进行服务器端配置。。作为替代方案,您可以尝试其他路由器模式吗?比如“散列”

以下是有关模式和服务器配置的文档。

https://router.vuejs.org/api/#mode

https://router.vuejs.org/guide/essentials/history-mode.html#example-server-configurations