npm install svg-sprite-loader@0.3.1 -D
- 删除配置文件.roadhogrc
- 添加.roadhogrc.js
const path = require('path');
const svgSpriteDirs = [
require.resolve('antd-mobile').replace(/warn\.js$/, ''), // antd-mobile 内置svg
path.resolve(__dirname, 'src/assets'), // 业务代码本地私有 svg 存放目录
];
export default {
"entry": "src/index.js",
"env": {
"development": {
"extraBabelPlugins": [
"dva-hmr",
"transform-runtime",
["import", { "libraryName": "antd-mobile", "style": "css" }]
]
},
"production": {
"extraBabelPlugins": [
"transform-runtime",
["import", { "libraryName": "antd-mobile", "style": "css" }]
]
}
},
// ...
svgSpriteLoaderDirs: svgSpriteDirs,
//...
}
const CustomIcon = ({ type, className = '', size = 'md', ...restProps }) => {
return (
<svg
className={`am-icon am-icon-${type.default.id} am-icon-${size} ${className}`}
{...restProps}
>
<use xlinkHref={`#${type.default.id}`} />
</svg>
);
};
<CustomIcon type={require('../assets/activity.svg')} />