vue-router进行build无法正常显示路由页面

  1. 使用vue cli创建一个webpack工程
  2. 加入vue-router然后使用路由引入一个新的组件。这时路由和链接是这样写的
const router = new VueRouter({
  mode: 'history',
  base: __dirname,
  routes: [
    {
      path: '/first',
      component: firstCom
    }
  ]
})
<a href="/first">Try this!</a>
  1. npm run dev查看没有问题
  2. npm run build打包
  3. 起一个服务(例如:python -m SimpleHTTPServer)然后查看index.html页面,发现路由会请求/first页面。
  4. 解决的办法:将路由配置中history改为hash,将链接中/first改为/#/first。问题解决。

============2017.8.24更新================
又找了点资料发现,其实router的mode使用history是可以的。是我在做跳转的时候出现了问题。我想当然的使用了window.location.href=”“,其实应该使用router.push。代码里面的handleSelect是因为使用了element ui出现的一个消息处理方法。可以理解为当按键点击时触发该方法,如果按键的key是2,那么跳转到first,key是3跳转到second。

<script>
  export default {
    data () {
      return {
      }
    },
    methods: {
      handleSelect(key, keyPath) {
        if (key == 2){
          this.$router.push('first');
        } else if (key == 3){
          this.$router.push('second');
        }
      }
    }
  }
</script>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值