[英]React router not allowing images to load
我第一次使用react-router,我的项目遇到了一些问题。 React-router很好地更改了URL,但是随后我的图像没有被加载。 我相信这是因为基本url发生了变化,例如,当链接是这样时,它可以工作: http://localhost:3000/f0287893b2bcc6566ac48aa6102cd3b1.png
但在这样的http://localhost:3000/module/f0287893b2bcc6566ac48aa6102cd3b1.png
http://localhost:3000/f0287893b2bcc6566ac48aa6102cd3b1.png
没有http://localhost:3000/module/f0287893b2bcc6566ac48aa6102cd3b1.png
。 这是我的路由器代码:
import { Router, Route, browserHistory, IndexRoute } from 'react-router'
import { syncHistoryWithStore } from 'react-router-redux'
import { Provider } from 'react-redux'
import ReactDOM from 'react-dom'
import React from 'react'
import App from './containers/App'
import configure from './store'
import Home from './components/home';
import Module from './components/module-page/module';
import Login from './components/login/login';
const store = configure();
const history = syncHistoryWithStore(browserHistory, store);
ReactDOM.render(
<Provider store={store}>
<Router history={history}>
<Route path="/" component={App}>
<IndexRoute component={Login} />
<Router path="/home" component={Home}/>
<Router path="/module(/:module)" component={Module}/>
</Route>
</Router>
</Provider>,
document.getElementById('root')
)
这是触发链接的地方:
import React, { Component } from 'react';
import { ROUTE_MODULE } from '../../constants/Routes';
import { Link } from 'react-router';
import styles from './side-bar-items.css';
import { Navbar, Nav, NavItem, Collapse } from 'react-bootstrap/lib'
import FontAwesome from 'react-fontawesome';
class SideBarItems extends Component {
constructor(prop) {
super(prop);
this.state = { open: false };
}
render() {
return (
<Navbar.Collapse>
<Nav>
<NavItem className={styles.navItem} eventKey={1}>
<FontAwesome className={styles.navItemIcon} name='globe' size="lg"/>
<span className={styles.navItemIconText}>Dashboard</span>
</NavItem>
<NavItem onClick={this.showModules} className={`${styles.navItem} ${styles.itemModule}`}>
<FontAwesome className={styles.navItemIcon} name='clone' size="lg"/>
<span className={styles.navItemIconText}>Modules</span>
<Collapse in={this.state.open}>
<div className={styles.collapse}>
<div className={styles.module}>
<Link to="/module/moduleCode=2999">Programming III</Link>
</div>
</div>
</Collapse>
</NavItem>
</Nav>
</Navbar.Collapse>
)
}
showModules = () => {
this.setState({ open: !this.state.open })
}
}
export default (SideBarItems);
这是我导入图像的位置:
从'react'导入React,{组件}; 从'react-bootstrap / lib / Image'导入图像从'../../images/avatar.jpg'导入化身; 从'./user-image.css'导入样式;
class UserImage extends Component {
render() {
return (
<div className={styles.userContainer}>
<Image className={styles.avatar} src={avatar} rounded />
<span className={styles.userName}>Hello, Daniel</span>
<span className={styles.userCourse}>SEC, Software Engineering</span>
</div>
)
}
}
export default (UserImage);
这是我单击链接时网站的外观
为了确保映像是从服务器的根目录而不是当前路由的相对目录中获取的,请在src
前面添加一个/
:
<Image className={styles.avatar} src={`/${avatar}`} rounded />
对我有用的是将HTML基本引用设置为Web根
<head>
<base href="/">
</head>
当以预定路径刷新页面时,这还解决了路由器的其他一些问题。
也许您可以在HTML文件中设置基本元素。
<base href="http://www.example.com/page.html">
您应该在public / index.html中添加,以便可以从基本服务器而不是相对路径中加载图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.