Vue-router 页面刷新或URL访问
Vue-router page refresh or URL access
我有一个带有 register/login 的登陆页面,从该页面重定向到包含内容的主页。成功登录后,我这样重定向:
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 错误。
您还必须为此进行服务器端配置。作为替代方案,您可以尝试其他路由器模式吗?喜欢 "hash"
这里是模式和服务器配置的文档。
https://router.vuejs.org/api/#mode
https://router.vuejs.org/guide/essentials/history-mode.html#example-server-configurations
我通过添加解决了
Route::get('/{vue_capture?}', function () { return view('welcome'); })->where('vue_capture', '[\/\w\.-]*');
进入web.php文件
我有一个带有 register/login 的登陆页面,从该页面重定向到包含内容的主页。成功登录后,我这样重定向:
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 错误。
您还必须为此进行服务器端配置。作为替代方案,您可以尝试其他路由器模式吗?喜欢 "hash"
这里是模式和服务器配置的文档。
https://router.vuejs.org/api/#mode
https://router.vuejs.org/guide/essentials/history-mode.html#example-server-configurations
我通过添加解决了
Route::get('/{vue_capture?}', function () { return view('welcome'); })->where('vue_capture', '[\/\w\.-]*');
进入web.php文件