vue出現(xiàn)白屏的3種情況:1、把路由模式mode設(shè)置成history了;只需改為hash或者直接刪除模式配置,如果非要用history的話,在服務(wù)端加一個覆蓋所有情況的候選資源即可。2、打包后的dist目錄下的文件引用路徑不對,會因找不到文件而報錯導(dǎo)致白屏;修改一下config下面的index.js中bulid模塊導(dǎo)出的路徑即可。3、項目中用了es6語法,但瀏覽器不支持es6。
前端(vue)入門到精通課程,老師在線輔導(dǎo):聯(lián)系老師
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API調(diào)試工具:點擊使用
本教程操作環(huán)境:windows7系統(tǒng)、vue3版,DELL G3電腦。
vue出現(xiàn)白屏現(xiàn)象主要幾種原因和解決辦法
第一種:由于把路由模式mode設(shè)置成history了,默認(rèn)是hash。
解決方法:改為hash或者直接刪除模式配置,如果非要用的話,在服務(wù)端加一個覆蓋所有情況的候選資源。
如果你改成了history模式的話,打開也會是一片空白。所以改為hash或者直接把模式配置刪除,讓它默認(rèn)的就行 。如果非要使用history模式的話,需要你在服務(wù)端加一個覆蓋所有的情況的候選資源:如果URL匹配不到任何靜態(tài)資源,則應(yīng)該返回一個index.html,這個頁面就是你app依賴頁面。
第二種:打包后的dist目錄下的文件引用路徑不對,會因找不到文件而報錯導(dǎo)致白屏。
解決辦法:修改一下config下面的index.js中bulid模塊導(dǎo)出的路徑。
因為index.html里邊的內(nèi)容都是通過script標(biāo)簽引入的,而你的路徑不對,打開肯定是空白的。先看一下默認(rèn)的路徑。
build: { // Template for index.html index: path.resolve(__dirname, '../dist/index.html'), // Paths assetsRoot: path.resolve(__dirname, '../dist'), assetsSubDirectory: 'static', assetsPublicPath: './', /** * Source Maps */ productionSourceMap: false, // https://webpack.js.org/configuration/devtool/#production devtool: '#source-map', // Gzip off by default as many popular static hosts such as // Surge or Netlify already gzip all static assets for you. // Before setting to `true`, make sure to: // npm install --save-dev compression-webpack-plugin productionGzip: false, productionGzipExtensions: ['js', 'css'], // Run the build command with an extra argument to // View the bundle analyzer report after build finishes: // `npm run build --report` // Set to `true` or `false` to always turn it on or off bundleAnalyzerReport: process.env.npm_config_report }
assetsPublicPath默認(rèn)的是 ‘/’ 也就是根目錄。而我們的index.html和static在同一級目錄下面。 所以要改為 ‘./ ’;
如果還是報錯,修改build/webpack.prod.conf.js文件下webpackConfig,在output屬性中添加 publicPath:"./",重新運行打包。
output: { path: config.build.assetsRoot, filename: utils.assetsPath('js/[name].[chunkhash].js'), chunkFilename: utils.assetsPath('js/[id].[chunkhash].js') }
第三種:在項目中使用了es6的語法,一些瀏覽器不支持es6,造成編譯錯誤不能解析而造成白屏
解決方法:安裝Babel ,Babel 會把這些新語法轉(zhuǎn)譯成較低版本的代碼。
npm install --save-dev @babel/core @babel/cli @babel/preset-env
【學(xué)習(xí)視頻分享:vue視頻教程、web前端視頻】