使用vue cli創建一個webpack工程
加入vue-router然后使用路由引入一個新的組件。這時路由和鏈接是這樣寫的
const router = new VueRouter({ mode: 'history', base: __dirname, routes: [ { path: '/first', component: firstCom } ] })
<a href="/first" rel="external nofollow" >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>
以上這篇解決vue-router進行build無法正常顯示路由頁面的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com