react build后直接從瀏覽器打開


存在兩個問題

一,資源文件路徑

config/paths.js

這里原來的.pathname:'/', 改成.pathname:'./'

function getServedPath(appPackageJson) {
  const publicUrl = getPublicUrl(appPackageJson);
  const servedUrl =
    envPublicUrl || (publicUrl ? url.parse(publicUrl).pathname : './');
  return ensureSlash(servedUrl, true);
}

由此,背景圖片的地址可能是

.add-bank-sign {
    background: url("./../img/index.png"); //路徑前面加'./'
}

二,當點擊路由跳轉的時候,會出現這樣的報錯

解決方案:

routes/index.js

把原來的

import { BrowserRouter as Router,Route, Switch, Redirect } from 'react-router-dom'

改成:

import { HashRouter as Router, Route, Switch, Redirect } from 'react-router-dom'

就可以了

當然開發的時候,最好BrowserRouter


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM