Gatsby如何在sss中正确导入svg作为背景?

时收到错误消息Can't resolve '../../images/hero.svg'
  1. 我正在尝试从images folder

  2. 导入svg图像
  3. 并将其设置在header.scss中作为背景图像:background-image: url('../../images/hero.svg');

我的盖茨比结构:

├─ src
├── scss
│   ├── main.scss
│   ├── modules
│   └──── header.scss
└── images
    └── hero.svg

gatsby-config.js:

module.exports = {
  siteMetadata: {
    title: 'Test',description: 'test',author: '@gatsbyjs'
  },plugins: [
    'gatsby-plugin-react-helmet',{
      resolve: 'gatsby-source-filesystem',options: {
        name: 'images',path: `${__dirname}/src/images`
      }
    },'gatsby-transformer-sharp','gatsby-plugin-sharp',{
      resolve: 'gatsby-plugin-manifest',options: {
        name: 'Test',short_name: 'test',start_url: '/',background_color: '#663399',theme_color: '#663399',display: 'minimal-ui',icon: 'src/images/android-chrome-192x192.png'
      }
    },'gatsby-plugin-sass',{
      resolve: 'gatsby-plugin-react-svg',options: {
        rule: {
          include: /\.inline\.svg$/
        }
      }
    }
    // this (optional) plugin enables Progressive Web App + Offline functionality
    // To learn more,visit: https://gatsby.dev/offline
    // `gatsby-plugin-offline`,]
}

谢谢。

heartear15 回答:Gatsby如何在sss中正确导入svg作为背景?

通过创建static文件夹并将svg文件放入其中解决了问题。还需要将scss文件中的路径更新为'/hero.svg'

,

从'/hero.svg'导入heroSvg;

在js等中的css,scss或css中

 background-image: url(${heroSvg});
 background-repeat: no-repeat;
本文链接:https://www.f2er.com/3051631.html

大家都在问