我使用create-react-app創建了一個應用程序,同時使用npm start運行開發代碼,所有css都可以正常加載頁面,但是當我使用npm運行build構建項目時,css無法加載。下面是附上的截圖和代碼片段。
正在開發中加載的頁面。
提供構建文件夾時的同一頁面。
下面是App.js導入
import React, { useEffect, useState } from "react";
import "bootstrap/dist/css/bootstrap.min.css";
import "jquery/dist/jquery.min.js";
import "bootstrap/dist/js/bootstrap.min.js";
import "./assets/css/classy-nav.min.css";
import "./assets/vendors/font-awesome/v5.9.0/all.css";
import "./assets/vendors/animate-css/animate.css";
import "./assets/vendors/magnify-popup/magnific-popup.css";
import "./assets/css/style.css";
import "owl.carousel/dist/assets/owl.carousel.css";
import "owl.carousel/dist/assets/owl.theme.default.css";
import { toast, ToastContainer } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import $ from "jquery";
import { WOW } from "wowjs";
toast.configure();
您可能只需要在npm安裝后運行npm start。
同樣的問題,用按需注入樣式解決:https://fk hadra . github . io/react-toastify/how-to-style # inject-style-on-demand(而不是import & quotreact-toastify/dist/react toastify . CSS & quot;;)
import { injectStyle } from "react-toastify/dist/inject-style";
// CALL IT ONCE IN YOUR APP
injectStyle();