javascript - React 路由器不允许加载图像

标签 javascript reactjs react-router

我是第一次使用 react-router,我的项目有点问题。 React-router 可以很好地更改 url,但是我的图像没有加载。我相信这是因为基本 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')
)

这是触发链接的地方:

  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);

这是我导入图片的地方:

import React, { Component } from 'react';
import Image from 'react-bootstrap/lib/Image'
import avatar from '../../images/avatar.jpg';
import styles from './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);

这是我点击链接时网站的样子

image

最佳答案

对我有用的是将 HTML 基本引用设置为 Web 根目录

<head>
    <base href="/">
</head>

这也解决了在预定路由刷新页面时路由器的一些其他问题。

关于javascript - React 路由器不允许加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42837241/

相关文章:

javascript - 如何从一个js文件访问另一个js文件中的对象?

javascript - Ant 设计表单initialValue如何使用

reactjs - window.history.back() 没有被调用 - Jest enzyme

reactjs - React - 组件不会在 foreach 循环中呈现?

javascript - 使用 lodash 检查属性是否有值(value)

javascript - 为什么 setInterval 对我创建的对象方法不起作用?

php - 无法从 PHP json_encode 解析 JS 中的 JSON 对象

javascript - 如何从标题包含给定单词的 json 文件中分离出所有标题?

reactjs - react route 的重复路径

reactjs - 在 React 中实现嵌套异步路由获取