导入 .svg 并调整 .svg 大小在 next.js 中使用样式组件
我目前正在尝试将 .svg 导入 next.js 项目,但失败了。
我尝试以与 React 项目相同的方式导入 .svg,创建了typing.d.ts 并导入 svg 之类的组件。但这不起作用。
declare module '*.svg' {
import React from 'react';
const src: string;
export const ReactComponent: React.FunctionComponent<React.SVGProps<SVGSVGElement>>;
export default src;
}
导入 svg 示例
- 我使用 next.js - 12.1 和 styled-components - 5.3.3
- example-import-svg
我需要什么?
- 导入
.svg
文件并更改其宽度和高度。
有人遇到过同样的问题吗? 或者有人设法解决了吗?
谢谢
I am currently trying import .svg into next.js project but I failed.
I tried import .svg same way as in react project, that I created typing.d.ts and import svg like component. But it doesn't work.
declare module '*.svg' {
import React from 'react';
const src: string;
export const ReactComponent: React.FunctionComponent<React.SVGProps<SVGSVGElement>>;
export default src;
}
Import svg example
- I using next.js - 12.1 and styled-components - 5.3.3
- example-import-svg
What I need?
- Import
.svg
file and change its width and height.
Has anyone had the same problem?
Or did anyone manage to solve it?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许是一个解决方案。
removeViewBox
的默认选项是true
。如果您更改 .svgheight
或width
,.svg
中的视图框将恢复为默认值。next.config.js
webpack 中使用并将removeViewBox
设置为false
.svg 调整大小即可开始工作。一个工作示例: stackblitz-resize-svg-next.js
如果您在
.babelrc
中设置了“inline-react-svg”
,则此解决方案将不起作用。Maybe a solution.
removeViewBox
istrue
. If you change in .svgheight
orwidth
the view-box from.svg
will be rest to default value.next.config.js
webpack and setremoveViewBox
tofalse
.svg resize start work.A working example: stackblitz-resize-svg-next.js
If you have in
.babelrc
set"inline-react-svg"
this solution will not work.我使用的另一种方法是利用 svgo-loader:
Another approach I've used, leverages
svgo-loader
:我就是这样用的。
1-我使用 SVG 代码创建了一个组件,该组件接收宽度、高度或其中任何一个作为参数
2-如果没有它们,则获取 svg 默认宽度和值,否则它将返回具有新宽度的 svg或高度值,根据给定的
3- 默认值添加到 viewBox 属性
正如您可以看到下面的代码:
}
这样我就可以将其导入到任何组件或页面中并在以下位置使用它:
1- 新宽度:
2- 新高度:
3- 或默认宽度和高度
I am using like that.
1- I created a Component with the SVG code that receives as a parameter width, height, or none of them
2- If it has none of them, gets the svg default width and value, else it will return the svg with its new width or height value, according to the one given
3- The default value is added to the viewBox attribute
As you can see the code below:
}
That way i can import it in any component or page and use it in:
1- New widths:
2- New heights:
3- Or default width and height