src/images create-react-app 中的文件夹
src/images folder in create-react-app
我有一个使用 create-react-app
构建的应用程序。我想要这样的文件夹结构:
src/
components/
CustomAppBar.js
images/
logo.svg
App.js
index.tsx
images.d.ts
目前我想使用 CustomAppBar.js
中图像文件夹中的 logo.svg
。当前此文件如下所示:
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import Button from '@material-ui/core/Button';
import IconButton from '@material-ui/core/IconButton';
import MenuIcon from '@material-ui/icons/Menu';
import logo from '*.svg';
const styles = {
flex: {
flex: 1,
},
menuButton: {
marginLeft: -12,
marginRight: 20,
},
root: {
flexGrow: 1,
},
};
function ButtonAppBar(props) {
const { classes } = props;
return (
<div className={classes.root}>
<AppBar position="static" title={<img styles={{height: '50px'}} src={logo} />}>
<Toolbar>
<IconButton className={classes.menuButton} color="inherit" aria-label="Menu">
<MenuIcon />
</IconButton>
<Typography variant="title" color="inherit" className={classes.flex}>
Title
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
</div>
);
}
ButtonAppBar.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(ButtonAppBar);
正如预期的那样,这失败了:
Module not found: Can't resolve '*.svg' in 'some\path\src\components
images.d.ts
的内容为库存标准:
declare module '*.svg'
declare module '*.png'
declare module '*.jpg'
我在其他地方发现了一些提到修改 Webpack 配置的建议。似乎 create-react-app
隐藏了 Webpack 配置的东西。这方面的最佳做法是什么?
我通常对 /public/assets
下的资产执行的操作我导入我的文件,然后在我的反应组件中使用 src 我可以使用 process.env.PUBLIC_URL + '/assets/{ENTER REST OF PATH HERE}'
访问它们
这是我如何实现它的代码示例。
import React, { Component } from 'react';
const iconPath = process.env.PUBLIC_URL + '/assets/icons/';
export default class TestComponent extends Component {
constructor(props) {
super(props);
}
render(){
return (<img
src={`${iconPath}icon-arrow.svg`}
alt="more"
/>)
}
}
这是让我开始以这种方式实施它的 link。
https://github.com/facebook/create-react-app/issues/2854
我还注意到您导入的徽标不正确,应该是 import logo from '../images/logo.svg'
或者如果 logo.svg 没有导出默认值,您应该使用 import {logo} from '../images/logo.svg'
您可以使用 ES6 导入从根文件夹中找到任何文件(图像、音频等)并将它们添加到您的应用中。
import React from 'React'
import errorIcon from './assets/images/errorIcon.svg'
import errorSound from './assets/sounds/error.wav'
class TestComponent extends React.Component
{
render()
{
return (
<div>
<img src={ errorIcon }
alt="error Icon" />
<audio src={ errorSound } />
</div>
)
}
}
我有一个使用 create-react-app
构建的应用程序。我想要这样的文件夹结构:
src/
components/
CustomAppBar.js
images/
logo.svg
App.js
index.tsx
images.d.ts
目前我想使用 CustomAppBar.js
中图像文件夹中的 logo.svg
。当前此文件如下所示:
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import Button from '@material-ui/core/Button';
import IconButton from '@material-ui/core/IconButton';
import MenuIcon from '@material-ui/icons/Menu';
import logo from '*.svg';
const styles = {
flex: {
flex: 1,
},
menuButton: {
marginLeft: -12,
marginRight: 20,
},
root: {
flexGrow: 1,
},
};
function ButtonAppBar(props) {
const { classes } = props;
return (
<div className={classes.root}>
<AppBar position="static" title={<img styles={{height: '50px'}} src={logo} />}>
<Toolbar>
<IconButton className={classes.menuButton} color="inherit" aria-label="Menu">
<MenuIcon />
</IconButton>
<Typography variant="title" color="inherit" className={classes.flex}>
Title
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
</div>
);
}
ButtonAppBar.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(ButtonAppBar);
正如预期的那样,这失败了:
Module not found: Can't resolve '*.svg' in 'some\path\src\components
images.d.ts
的内容为库存标准:
declare module '*.svg'
declare module '*.png'
declare module '*.jpg'
我在其他地方发现了一些提到修改 Webpack 配置的建议。似乎 create-react-app
隐藏了 Webpack 配置的东西。这方面的最佳做法是什么?
我通常对 /public/assets
下的资产执行的操作我导入我的文件,然后在我的反应组件中使用 src 我可以使用 process.env.PUBLIC_URL + '/assets/{ENTER REST OF PATH HERE}'
这是我如何实现它的代码示例。
import React, { Component } from 'react';
const iconPath = process.env.PUBLIC_URL + '/assets/icons/';
export default class TestComponent extends Component {
constructor(props) {
super(props);
}
render(){
return (<img
src={`${iconPath}icon-arrow.svg`}
alt="more"
/>)
}
}
这是让我开始以这种方式实施它的 link。 https://github.com/facebook/create-react-app/issues/2854
我还注意到您导入的徽标不正确,应该是 import logo from '../images/logo.svg'
或者如果 logo.svg 没有导出默认值,您应该使用 import {logo} from '../images/logo.svg'
您可以使用 ES6 导入从根文件夹中找到任何文件(图像、音频等)并将它们添加到您的应用中。
import React from 'React'
import errorIcon from './assets/images/errorIcon.svg'
import errorSound from './assets/sounds/error.wav'
class TestComponent extends React.Component
{
render()
{
return (
<div>
<img src={ errorIcon }
alt="error Icon" />
<audio src={ errorSound } />
</div>
)
}
}