溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

vue中熱替換失效的原因是什么

發(fā)布時間:2022-04-21 17:32:14 來源:億速云 閱讀:195 作者:zzz 欄目:大數(shù)據(jù)

本文小編為大家詳細介紹“vue中熱替換失效的原因是什么”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“vue中熱替換失效的原因是什么”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。

1.觀察文件位置錯誤

{

 env: require('./dev.env'),
 port: 8080,
 autoOpenBrowser: true,
 assetsSubDirectory: 'static',//必須
 assetsPublicPath: '/',

失效是為什么?是因為修改了源代碼后,依然會立刻編譯,但是通常被觀察的新編譯的文件位置錯了。也就是說瀏覽器顯示的東西與服務器觀察的東西是一個位置,而編譯出來文件是另外的位置。

解決辦法是:config/index.js中 dev的這個參數(shù)必須為static

2.項目目錄包含特殊字符

像這樣的路徑 D:\(myworkspace)\vue-answer-project

這種目錄中有一個括號?。?!就有可能在瀏覽器中打開后,發(fā)現(xiàn)console報錯

http://localhost:8080/__webpack_hmr net::ERR_INCOMPLETE_CHUNKED_ENCODING

這是什么意思呢?就是熱替換模塊報錯,中斷了觀察頁面與熱替換模塊的鏈接,無法收到事件。

解決辦法就是:去掉這樣的路徑

3.npm run build后,打開瀏覽器一片空白

這個位置是根據(jù)文件webpack.config.js中的publicPath進行指定的。也就是服務器觀察位置是 publicPath: "XX/build.js"這里面的 /XX/build.js這個文件,這個文件需要你在文件 index.html中 正確引入。

// webpack編譯輸出的發(fā)布路徑
// => 將 build 的路徑前綴修改為 ' ./ '(原本為 ' / '),是因為打包(npm run build)之后,
// 外部引入 js 和 css 文件時,如果路徑以 ' / ' 開頭,在本地是無法找到對應文件的(服務器上沒問題)
module.exports = {
 build: {
 env: require('./prod.env'),
 index: path.resolve(__dirname, '../dist/index.html'),
 assetsRoot: path.resolve(__dirname, '../dist'),
 assetsSubDirectory: 'static',
 assetsPublicPath: './',
 productionSourceMap: true,
 // 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
 },
 dev: {
 env: require('./dev.env'),
 port: 8081,
 autoOpenBrowser: true,
 assetsSubDirectory: 'static',
 assetsPublicPath: '/',
 proxyTable: {},
 // CSS Sourcemaps off by default because relative paths are "buggy"
 // with this option, according to the CSS-Loader README
 // (https://github.com/webpack/css-loader#sourcemaps)
 // In our experience, they generally work as expected,
 // just be aware of this issue when enabling this option.
 cssSourceMap: true
 }

解決辦法是:在上圖中的build.assetsPublicPath的值 改為 "./"

讀到這里,這篇“vue中熱替換失效的原因是什么”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內(nèi)容的文章,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

vue
AI