【发布时间】: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