Dan*_*sah 5 javascript reactjs react-router
我第一次使用react-router,我的项目有点问题.React-router正在改变url,但是我的图像没有被加载.我相信这是因为基本网址发生了变化,例如它在链接时会起作用: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')
)
Run Code Online (Sandbox Code Playgroud)
这是触发链接的地方:
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);
Run Code Online (Sandbox Code Playgroud)
这是我导入图像的地方:
从'react'导入React,{Component}; 从'../../images/avatar.jpg'导入来自'react-bootstrap/lib/Image'的图像导入头像; 从'./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);
Run Code Online (Sandbox Code Playgroud)
这是我点击链接时网站的外观
Fab*_*ltz 12
要确保从服务器的根目录而不是当前路由的相对目录中获取映像,请/在以下位置添加src:
<Image className={styles.avatar} src={`/${avatar}`} rounded />
Run Code Online (Sandbox Code Playgroud)
对我有用的是将HTML基本引用设置为Web根
<head>
<base href="/">
</head>
Run Code Online (Sandbox Code Playgroud)
当以预定路径刷新页面时,这还解决了路由器的其他一些问题。
| 归档时间: |
|
| 查看次数: |
5486 次 |
| 最近记录: |