vue的router为history模式

一,出现的问题
1.路由跳转不起作用
(1)解决方法:
router

Vue.use(Router)
export default new Router({
mode:'history',  // 注意添加这一项
routes: [
     { path: '/', component: home},
     { path: '/content', component: Content },
        ]
})

注意要写上 mode:‘history’,

(2)原因:
参考:vue router官网配置 https://router.vuejs.org/zh/guide/essentials/history-mode.html
vue-router 默认 hash 模式 —— 使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载。
如果不想要很丑的 hash,我们可以用路由的 history 模式,这种模式充分利用 history.pushState API 来完成 URL 跳转而无须重新加载页面
也就是vue router将hash->history模式

2.转换成history后需要的支持(服务器的路由配置)
一开始不知道时,我是将所有的一级路由配置进去,比较麻烦
然后参考vue router官网配置 https://router.vuejs.org/zh/guide/essentials/history-mode.html
nginx配置:

location / {
  try_files $uri $uri/ /index.html  @router;
}
//刷新页面出现404问题 配置@router
 location @router {
            rewrite ^.*$ /index.html last;
    }

如果是其他方式如Apache等 请参考vue router官网配置 https://router.vuejs.org/zh/guide/essentials/history-mode.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值