【发布时间】:2018-06-11 08:32:34
【问题描述】:
我已经使用以下代码导入css
componentWillMount() {
import('./patient-summary.css');
}
如何在组件不使用时从 react 中删除导入的 css。当我回到前一个屏幕时,这个 css 被应用在那里。有什么想法吗?
更新:: Webpack 配置
const path = require('path');
const webpack = require('webpack');
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'public/dist')
},
module: {
rules: [
{
test: /\.js?$/,
loader: 'babel-loader',
exclude: /node_modules/
},
{
test: /\.css$/,
use: [ 'style-loader', 'css-loader' ]
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
loader: "file-loader"
}
,
{
test: /\.(png|jpeg|jpg|gif|svg)$/,
loader: "file-loader"
}
]
},
devServer: {
contentBase: path.resolve(__dirname, "public"),
historyApiFallback: true,
port: 3000,
watchOptions: {
// Delay the rebuild after the first change
aggregateTimeout: 300,
// Poll using interval (in ms, accepts boolean too)
poll: 1000,
},
},
plugins: [
// Ignore node_modules so CPU usage with poll
// watching drops significantly.
new webpack.WatchIgnorePlugin([
path.join(__dirname, "node_modules")
])
],
};
【问题讨论】:
-
请发布您的 webpack 配置。我认为您将所有 css 文件连接在一起。
-
为什么要删除导入。它们将在本地缓存中。让它在那里
-
@M.R.Safari 更新
-
所以...我猜你还没有找到任何解决方案
-
@Denny 我找到了。在组件顶部导入所有 css 文件。 webpack 中的 Atlast 将所有 css 构建到一个文件中。所以它不是单独的css文件。只有一个文件
标签: javascript css reactjs import redux