reactjs - 在 Next JS 中将图像导入我的 CSS(SASS) 文件
问题描述
我对 javascript 框架和 webpack 非常陌生,我只是想在我的 css 文件中导入图像。
这就是我的项目结构;
那是我的 next.config.js;
// next.config.js
const withCSS = require('@zeit/next-css');
const withSass = require('@zeit/next-sass');
const withImages = require('next-images');
module.exports = withCSS( withSass( withImages({
webpack (config, options) {
config.module.rules.push({
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
use: {
loader: 'url-loader',
options: {
limit: 100000
}
}
});
return config
}
})));
Main.scss 文件;
$font-type: 'Open Sans', sans-serif;
$primary-color: #ff5e1f;
body {
font-family: $font-type;
header {
float: left;
width: 100%;
min-height: 100px;
a.logo {
float: left;
width: 200px;
height: 100px;
background: url('../images/logo.png');
}
}
}
我只是在尝试使用我的徽标,终端没有任何问题;
但是当我访问我的网站和开发者工具时,网址是这样的;
background: url(/_next/static/images/logo-ea62a25….png);
当我在 Google Chrome 和 Firefox 中打开 URL 时,什么都没有显示(img 未加载);
The image "http://localhost:3000/_next/static/images/logo-ea62a2595859a68f77a6cc473535b8f5.png" canot be displayed because it contains errors.
也许这是一个愚蠢的问题,很抱歉,但我无法处理它:/我该如何解决这个问题?
解决方案
我刚刚找到了解决方案,我使用了 file-loader 而不是 url-loader。
我像这样更改了我的 next.config.js 文件;
// next.config.js
const withCSS = require('@zeit/next-css');
const withSass = require('@zeit/next-sass');
const withImages = require('next-images');
module.exports = withCSS( withSass( withImages({
module: {
rules: [
{
test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/i,
use: [
{
loader: 'file-loader',
},
],
},
],
},
})));
它可以工作:)(也不要忘记使用 npm 安装文件加载器)
npm install file-loader --save-dev
推荐阅读
- python-3.x - 属性错误:“Graph”对象没有属性“vcount”
- wavelet - 寻找具有许多样本的一维数据的计算上可行的小波变换算法和实现
- python - 我无法尝试使用包含列名称的列表从数据框中选择列
- mysql - 尝试在 Ubuntu 18.04 中使用 MYSQL 时遇到问题
- sql-server - 如何将数据库复制到另一台服务器到 SQL Server 中的特定磁盘上
- c# - C# - 如何为 Refelctions 获取的字段的子项设置值
- c++ - 标头的有效性包含在外部块内吗?
- eclipse - 当在孩子的pom中声明时,eclipse不会在module-child项目中导入POI依赖项,但是在paren't pom上是
- python - 如何“清理”给定格式的字符串数据,以便可以从中创建字典?
- javascript - 查找并推送结果,将两个对象推送到数组中