vue-router 按需加載 component: () => import() 報錯的解決
vue的單頁面(SPA)項目,必然涉及路由按需的問題。
以前我們是這么做的
//require.ensure是webpack里面的,這樣做會將單獨(dú)拉出來作為一個chunk文件
const Login = r => require.ensure( [], () => r (require('../component/Login.vue')));
但現(xiàn)在vue-router的官網(wǎng)看看,推薦這種方式:
//vue異步組件和webpack的【代碼分塊點(diǎn)】功能結(jié)合,實(shí)現(xiàn)了按需加載
const App = () => import('../component/Login.vue');
可是,很多情況下,我們這么寫npm run dev控制臺直接報錯,這是為什么呢?
Module build failed: SyntaxError: Unexpected token
原來是import這兒報錯了,這就需要babel的插件了,vue-router官網(wǎng)上有一段提示:
如果您使用的是 Babel,你將需要添加 syntax-dynamic-import 插件,才能使 Babel 可以正確地解析語法。
至此,問題全部解決了。
如果使用vue-cli生成項目,很可能在babel-loader沒有配置上面的插件,這時需要我們自己去安裝此插件:
cnpm install babel-plugin-syntax-dynamic-import --save-dev
然后修改webpack的js的loader部分:
{
test: /\.js$/,
loader:'babel-loader',
options:{
plugins:['syntax-dynamic-import']
},
},
增加了option選項,至此,能識別我們:
const App = () => import('../component/Login.vue');
的語法了,頁面出來了:

在打包的時候,發(fā)現(xiàn)我們?nèi)绻皇沁@么寫,出現(xiàn)的chunk包名字都是亂的,如果我們指定命名,該怎么辦呢?webpack3提供了Magic Comments(魔法注釋)
const App = () => import(/* webpackChunkName:'login'*/ '../component/Login.vue');
這樣我們就為打包出來的chunk指定一個名字,最終生成login.js的chunk包。
補(bǔ)充知識:Vue根據(jù)是否授權(quán),跳轉(zhuǎn)不同的路由(vue-router動態(tài)路由)
功能點(diǎn):項目一運(yùn)行需要先請求后臺,根據(jù)后臺返回結(jié)果跳轉(zhuǎn)對應(yīng)路由,如果用戶已經(jīng)授權(quán)跳轉(zhuǎn)首頁,如果用戶沒有授權(quán),跳轉(zhuǎn)授權(quán)頁面進(jìn)行授權(quán)。
實(shí)現(xiàn)代碼如下:
router文件夾中的index.js
import Vue from "vue";
import Router from "vue-router";
Vue.use(Router);
let router =new Router({
routes:[]
});
//全局路由鉤子函數(shù)
router.beforeEach((to,from,next)=>{
//不加這個判斷,路由會陷入死循環(huán)重復(fù)添加路由
if(!to.name){
alert("請上傳有效的License文件,以正常使用系統(tǒng)功能");
next("/licenseManage");
}else{
next();
}
})
export default router;
router文件夾的accessRouters.js(定義好不同的路由)
import index from "@/views/index";
export default{
//已授權(quán)路由列表
hasAccessRouters:[
{
path:"/",
name:"index",
component:index,
redirect:"preIntegrationTest",
children:[
{
path:"/preIntegrationTest",
name:"preIntegrationTest",
components:{
listPage:resolve =>{
require(["@/views/pages/preIntegrationTest"],resolve)
}
},
props:{}
},{
path:"/about",
name:"about",
components:{
listPage:resolve =>{
require(["@/views/pages/about"],resolve)
}
},
props:{}
},{
path:"/help",
name:"help",
components:{
listPage:resolve =>{
require(["@/views/pages/help"],resolve)
}
},
props:{}
}
}
],
//沒有授權(quán)的路由列表
noAccessRouters:[
{
path:"/",
name:"index",
component:index,
redirect:"licenseManage",
children:[
{
path:"/licenseManage",
name:"licenseManage",
components:{
listPage:resolve =>{
require(["@/views/systemSetting/licenseManage"],resolve)
}
},
props:{}
}
}
]
}
]
}
store中的index.js定義倉庫中的變量
import Vue from "vue";
import Vuex from "vuex";
import mutations from "./mutations";
import actions from "actions";
Vue.use(Vuex);
const store = new Vuex.store({
state:{
axios:axios.create({baseURL:"",transformRequest:[]}),
authorized:false
},
getters:{},
mutations,
actions
});
export default store;
mutation.js定義改變狀態(tài)庫中authorized的值的方法并加載動態(tài)路由
import router from "@/router/index";
import accessRouters from "@/router/accessRouters";
const mutations={
setAuthorized(state,payload){
if(payload){
//已授權(quán)
//加載路由為已授權(quán)定義的路由列表并跳轉(zhuǎn)/preIntegrationTest
router.addRoutes(accessRouters.hasAccessRouters);
let url=location.hash.substring(1)==='/'?'/preIntegrationTest':location.hash.substring(1);
router.push('url');
}else{
//沒有授權(quán),跳轉(zhuǎn)授權(quán)頁面
router.push('/licenseManage');
}
//更改狀態(tài)值
state.authorized=payload;
}
}
export default mutations;
action.js請求后臺接口返回結(jié)果,賦值給store狀態(tài)庫中的變量
const actions={
addInterceptors({state,commit}){
//響應(yīng)攔截--配置請求回來的信息
state.axios.interceptors.response.use(
function(response){
// 處理響應(yīng)數(shù)據(jù)
return response
},function(error){
if(error.response.status === 403){
commit("setAuthorized",false)
}
//處理響應(yīng)失敗
return Promise.reject(error)
}
)
},
setAuthorized({dispatch,state}){
dispatch("addInterceptors");
state.axios.get('url****').then(function(response){
if(response.data.code === "1"){
state.authorized = true;
router.addRoutes(accessRouters.hasAccessRouters);
let url=location.hash.substring(1)==="/"?"/preIntegrationTest":location.hash.substring(1);
router.push(url);
}else{
//沒有授權(quán) 跳轉(zhuǎn)授權(quán)頁面
state.authorized = false;
router.addRoutes(accessRouters.noAccessRouters);
router.push("/licenseManage");
}
}).catch(function(error){
console.log(error)
//沒有授權(quán) 跳轉(zhuǎn)授權(quán)頁面
state.authorized = false;
router.addRoutes(accessRouters.noAccessRouters);
router.push("/licenseManage");
})
}
}
export default actions;
以上這篇vue-router 按需加載 component: () => import() 報錯的解決就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue.js如何在網(wǎng)頁中實(shí)現(xiàn)一個金屬拋光質(zhì)感的按鈕
這篇文章主要給大家介紹了關(guān)于vue.js如何在網(wǎng)頁中實(shí)現(xiàn)一個金屬拋光質(zhì)感的按鈕的相關(guān)資料,文中給出了詳細(xì)的實(shí)例代碼以及圖文將實(shí)現(xiàn)的方法介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
vue3?+?async-validator實(shí)現(xiàn)表單驗(yàn)證的示例代碼
表單驗(yàn)證可以有效的過濾不合格的數(shù)據(jù),減少服務(wù)器的開銷,并提升用戶的使用體驗(yàn),今天我們使用?vue3?來做一個表單驗(yàn)證的例子,需要的朋友跟隨小編一起學(xué)習(xí)下吧2022-06-06
vue實(shí)現(xiàn)添加標(biāo)簽demo示例代碼
本篇文章主要介紹了vue實(shí)現(xiàn)添加標(biāo)簽demo示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
vue.js+element-ui的基礎(chǔ)表單實(shí)例代碼
這篇文章主要介紹了vue.js+element-ui的基礎(chǔ)表單實(shí)例代碼,技術(shù)棧即html+vue.js+element-ui,而使用它們的方法也很簡單,引入對應(yīng)的js和css文件即可,需要的朋友可以參考下2024-03-03
Vue中數(shù)組與對象修改觸發(fā)頁面更新的機(jī)制與原理解析
這篇文章主要介紹了Vue中關(guān)于數(shù)組與對象修改觸發(fā)頁面更新的機(jī)制與原理簡析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
vue請求服務(wù)器數(shù)據(jù)后綁定不上的解決方法
今天小編就為大家分享一篇vue請求服務(wù)器數(shù)據(jù)后綁定不上的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
Vue驗(yàn)證碼60秒倒計時功能簡單實(shí)例代碼
這篇文章主要介紹了Vue驗(yàn)證碼60秒倒計時功能簡單實(shí)例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06

