vue發佈以後會出現白屏現象主要幾種緣由和解決辦法

第一種:因爲把路由模式mode設置成history了,默認是hash。

解決方法:路由裏邊router/index.js路由配置裏邊默認模式是hash,若是你改爲了history模式的話,打開也會是一片空白。因此改成hash或者直接把模式配置刪除,讓它默認的就行 。若是非要使用history模式的話,須要你在服務端加一個覆蓋全部的狀況的候選資源:若是URL匹配不到任何靜態資源,則應該返回一個index.html,這個頁面就是你app依賴頁面。css

第二種:打包後的dist目錄下的文件引用路徑不對,會因找不到文件而報錯致使白屏。

解決辦法:修改一下config下面的index.js中bulid模塊導出的路徑。由於index.html裏邊的內容都是經過script標籤引入的,而你的路徑不對,打開確定是空白的。先看一下默認的路徑。html

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默認的是  ‘/’  也就是根目錄。而咱們的index.html和static在同一級目錄下面。  因此要改成  ‘./ ’;webpack


若是仍是報錯,修改build/webpack.prod.conf.js文件下webpackConfig,在output屬性中添加 publicPath:"./",從新運行打包。es6

output: {
    path: config.build.assetsRoot,
    filename: utils.assetsPath('js/[name].[chunkhash].js'),
    chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
  }

第三種:在項目中使用了es6的語法,一些瀏覽器不支持es6,形成編譯錯誤不能解析而形成白屏

解決方法:web

1. 安裝 npm install --save-dev babel-preset-es2015npm

2. 安裝 npm install --save-dev babel-preset-stage-3瀏覽器

3. 在項目根目錄建立一個.babelrc文件  裏面內容 最基本配置是:babel

{
// 此項指明,轉碼的規則
"presets": [
// env項是藉助插件babel-preset-env,下面這個配置說的是babel對es6,es7,es8進行轉碼,而且設置amd,commonjs這樣的模塊化文件,不進行轉碼
["env", { "modules": false }],
// 下面這個是不一樣階段出現的es語法,包含不一樣的轉碼插件
"stage-2"
],
// 下面這個選項是引用插件來處理代碼的轉換,transform-runtime用來處理全局函數和優化babel編譯
"plugins": ["transform-runtime"],
// 下面指的是在生成的文件中,不產生註釋
"comments": false,
// 下面這段是在特定的環境中所執行的轉碼規則,當環境變量是下面的test就會覆蓋上面的設置
"env": {
// test 是提早設置的環境變量,若是沒有設置BABEL_ENV則使用NODE_ENV,若是都沒有設置默認就是development
"test": {
"presets": ["env", "stage-2"],
// instanbul是一個用來測試轉碼後代碼的工具
"plugins": ["istanbul"]
}
}
}

而後重啓npm run dev  你會發現,能夠在其餘低版本瀏覽器跑了,基本兼容全部瀏覽器,ie8如下除外。並且大多數的手機瀏覽器也ok。從新打包到正式環境也正常。app

相關文章
相關標籤/搜索