【问题标题】:Unable to load second level routing page in vuejs after refreshing it刷新后无法在vuejs中加载二级路由页面
【发布时间】:2018-08-17 09:48:19
【问题描述】:

我的 VueJS 应用正在运行。它工作正常,但唯一的问题是,当我尝试刷新二级或三级路由页面时,它会在控制台中显示一条错误消息,说明:

Failed to load resource: the server responded with a status of 404 ()

例如,

http://localhost:8080 呈现我的主页。刷新没问题。

http://localhost:8080/details 呈现详细信息页面。刷新没问题。

http://localhost:8080/details/users 呈现用户页面。刷新时,它会出错。

我也为http://localhost:8080/details/assets 和http://localhost:8080/details/groups 定义了类似的链接,所有错误都会在页面刷新时出现。 这是main.js的代码:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App.vue'

import { routes } from './routes';


Vue.use(VueRouter);

//Vue.config.productionTip = false

const router = new VueRouter({
  mode: 'history',
  routes  
});

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
new Vue(Vue.util.extend({ router }, App)).$mount('#app');

对于routes.js:

import Home from './components/summary.vue'
import Details from './components/details/details.vue'
import Users from './components/details/users/users.vue'
import Assets from './components/details/assets/assets.vue'
import Groups from './components/details/groups/groups.vue'

export const routes = [
    { path: '/', component: Home},
    { path: '/details', component: Details},
    {path: '/details/users', component: Users},
    {path: '/details/groups', component: Groups},
    {path: '/details/assets', component: Assets}
];

知道如何解决这个问题吗?

【问题讨论】:

  • it shows me an error message我们需要猜测错误信息是什么吗?
  • @JaromandaX:更新了错误信息。我之前忘了包括它。我的错。
  • 那是什么服务器?您如何运行该服务器? npm run dev?
  • 是的,它只是npm ,但我正在用弹簧靴弹跳它。但我想这不应该改变路由器的行为。
  • 这是服务器端问题。这些路由只是前端路由,在服务器上根本不可用。我猜你没有使用 vue-cli ?如果您能够控制此服务器的 404 页面的模板,来自github.com/rafrex/spa-github-pages 的脚本会有所帮助。

标签: javascript vue.js vue-component vue-router


【解决方案1】:

如vue-router官方文档中所述

为了摆脱散列,我们可以使用路由器的历史模式,它利用 history.pushState API 实现 URL 导航而无需重新加载页面

所以只在前端导航不会向后端发送请求,后端是你的spring boot应用(你可以从网络流量中看到)

但是,当您刷新页面时,请求会直接发送到后端,从而导致 404 错误。

要解决您的问题,一个选项可能是将默认 404 页面设置为 index.html,建议使用 here

@Component
public class WebConfiguration implements EmbeddedServletContainerCustomizer {
    @Override
    public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
        configurableEmbeddedServletContainer.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/index.html"));
    }
}

希望它对你有用!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 2023-03-30
    • 2020-03-05
    • 2023-03-12
    • 2016-10-28
    相关资源
    最近更新 更多