【问题标题】:Vue-router page refresh or URL accessVue-router页面刷新或URL访问
【发布时间】:2019-02-19 07:07:51
【问题描述】:

我有一个带有注册/登录的登录页面,该页面重定向到包含内容的主页。成功登录后,我重定向如下:

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 错误。

【问题讨论】:

  • 您不应将 Home 组件用于路径 *。只需像在链接中添加的那样创建 NotFoundComponent 组件或类似的东西,它会向用户显示 404 错误。
  • 这与我的问题没有任何关系,因为 /main 确实存在

标签: vue.js vue-router


【解决方案1】:

我通过添加解决了它

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

进入 web.php 文件

【讨论】:

    【解决方案2】:

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

    这里是模式和服务器配置的文档。

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

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

    【讨论】:

    • 我已经按照帖子中的说明更改了 .htaccess(使用了您的第二个链接)。哈希方法确实有效,但这不是我想要的
    猜你喜欢
    • 2020-12-11
    • 2021-04-20
    • 2022-10-24
    • 2017-07-04
    • 1970-01-01
    • 2020-02-24
    • 2016-07-23
    • 2015-11-28
    • 2021-05-21
    相关资源
    最近更新 更多