Javascript 盖茨比在JS中设置背景图像CSS(情感)
使用下面的设置,没有可见的背景图像。作为调试步骤,我尝试在Javascript 盖茨比在JS中设置背景图像CSS(情感),javascript,css,reactjs,gatsby,emotion,Javascript,Css,Reactjs,Gatsby,Emotion,使用下面的设置,没有可见的背景图像。作为调试步骤,我尝试在const background中设置background:pink,这确实有效,确认emotion运行正常 打开React Dev Tools extension时,我可以看到背景图像:url(../images/page\u backgroundgradient.png),url(../images/page\u background.png)应用时没有错误 请问我的问题是什么 我的文件结构如下所示: frontend/ src/
const background
中设置background:pink
,这确实有效,确认emotion
运行正常
打开React Dev Tools extension
时,我可以看到背景图像:url(../images/page\u backgroundgradient.png),url(../images/page\u background.png)代码>应用时没有错误
请问我的问题是什么
我的文件结构如下所示:
frontend/
src/
images/
page_background.png
page_backgroundgradient.png
pages/
index.js
我的index.js
,我正在尝试向其添加背景图像
...
import { css, Global } from "@emotion/core"
const background = css`
background-image: url(../images/page_backgroundgradient.png), url(../images/page_background.png);
`
<div css={background}>
...
</div>
。。。
从“@emotion/core”导入{css,Global}
const background=css`
背景图像:url(../images/page_backgroundgradient.png),url(../images/page_background.png);
`
...
因此,正如您在评论中发布的链接所述,有多种方法可以将图像/资产包含在盖茨比中:
从graphql
导入
图像,获取路径
将图像复制到静态
目录
设置
假设您有这样一个组件:
// src/pages/sample.js
import React from 'react'
import { css } from '@emotion/core'
export default () => <div css={css`
width: 10rem;
height: 10rem;
background: url( ... );
`} />
export default () => <div css={css`
width: 10rem;
height: 10rem;
background: url(/image.png);
`} />
如链接中所述,查询字段publicURL
将为您提供文件名的路径:
export default ({ data }) => <div css={css`
width: 10rem;
height: 10rem;
background: url(${data.file ? data.file.publicURL : 'your/fallback.png'});
`} />
export const query = graphql`
query {
file(base: { eq: "image.png" }) {
publicURL
}
}
`
您可以在data.file.childImageSharp.fixed.src
访问它
导入图像
让webpack来处理它:
import myImagePath from '../relative/path/to/image.png';
export default () => <div css={css`
width: 10rem;
height: 10rem;
background: url(${myImagePath});
`} />
静态中的所有文件都将直接复制到生成中,因此您可以像这样链接到图像:
// src/pages/sample.js
import React from 'react'
import { css } from '@emotion/core'
export default () => <div css={css`
width: 10rem;
height: 10rem;
background: url( ... );
`} />
export default () => <div css={css`
width: 10rem;
height: 10rem;
background: url(/image.png);
`} />
导出默认值()=>
如果您在gatsby config.js
中使用pathPrefix
,请从gatsby
导入withPrefix
,并将其环绕图像路径
以下是前两种方法的示例
希望有帮助 因此,正如您在评论中发布的链接所述,有多种方式可以将图像/资产包含在盖茨比中:
从graphql
导入
图像,获取路径
将图像复制到静态
目录
设置
假设您有这样一个组件:
// src/pages/sample.js
import React from 'react'
import { css } from '@emotion/core'
export default () => <div css={css`
width: 10rem;
height: 10rem;
background: url( ... );
`} />
export default () => <div css={css`
width: 10rem;
height: 10rem;
background: url(/image.png);
`} />
如链接中所述,查询字段publicURL
将为您提供文件名的路径:
export default ({ data }) => <div css={css`
width: 10rem;
height: 10rem;
background: url(${data.file ? data.file.publicURL : 'your/fallback.png'});
`} />
export const query = graphql`
query {
file(base: { eq: "image.png" }) {
publicURL
}
}
`
您可以在data.file.childImageSharp.fixed.src
访问它
导入图像
让webpack来处理它:
import myImagePath from '../relative/path/to/image.png';
export default () => <div css={css`
width: 10rem;
height: 10rem;
background: url(${myImagePath});
`} />
静态中的所有文件都将直接复制到生成中,因此您可以像这样链接到图像:
// src/pages/sample.js
import React from 'react'
import { css } from '@emotion/core'
export default () => <div css={css`
width: 10rem;
height: 10rem;
background: url( ... );
`} />
export default () => <div css={css`
width: 10rem;
height: 10rem;
background: url(/image.png);
`} />
导出默认值()=>
如果您在gatsby config.js
中使用pathPrefix
,请从gatsby
导入withPrefix
,并将其环绕图像路径
以下是前两种方法的示例
希望有帮助 您是否尝试过在CSSurl()
函数中传递文件的完整路径?@guest271314,即:url(src/images/page_background.png)
?因为如果是这样,那就没有运气了!这不是完整路径。@guest271314我尝试了url(/Users/username/reponame/frontend/src/images/page_background.png)
,我相信这是完整路径。也不走运。生成时不会复制src
文件夹中的图像,因此使用的路径不会指向真实的图像文件。不过,您可能会在graphql查询中找到构建图像的路径,如果您仍在这一问题上,请告诉我。您是否尝试在CSSurl()
function?@guest271314处传递文件的完整路径,即:url(src/images/page\u background.png)
?因为如果是这样,那就没有运气了!这不是完整路径。@guest271314我尝试了url(/Users/username/reponame/frontend/src/images/page_background.png)
,我相信这是完整路径。也不走运。生成时不会复制src
文件夹中的图像,因此使用的路径不会指向真实的图像文件。不过,您很可能会在graphql查询中找到构建映像的路径,如果您仍然停留在这个问题上,请告诉我。@Dejell这也是最不理想的方法。您遇到了什么问题?在构建时,图片没有被复制到静态folder@Dejell这也是最不理想的方法。您遇到了什么问题?生成时,图片未复制到静态文件夹