Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React路由器不允许加载图像_Javascript_Reactjs_React Router - Fatal编程技术网

Javascript React路由器不允许加载图像

Javascript React路由器不允许加载图像,javascript,reactjs,react-router,Javascript,Reactjs,React Router,我第一次使用react路由器,我的项目有点小问题。React路由器正在更改url,但我的图像没有加载。我相信这是因为基本url发生了变化,例如,当链接如下所示时,它会工作:http://localhost:3000/f0287893b2bcc6566ac48aa6102cd3b1.png但当它像这样时就不会了http://localhost:3000/module/f0287893b2bcc6566ac48aa6102cd3b1.png。这是我的路由器代码: import { Router, R

我第一次使用react路由器,我的项目有点小问题。React路由器正在更改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')
)
从“react Router”导入{Router,Route,browserHistory,IndexRoute}
从“react router redux”导入{syncHistoryWithStore}
从“react redux”导入{Provider}
从“react dom”导入react dom
从“React”导入React
从“./containers/App”导入应用程序
从“/store”导入配置
从“./components/Home”导入Home;
从“./components/Module page/Module”导入模块;
从“./components/Login/Login”导入登录名;
const store=configure();
const history=syncHistoryWithStore(浏览器历史记录,存储);
ReactDOM.render(
,
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}来自'React';
从“../../constants/Routes”导入{ROUTE_MODULE};
从“反应路由器”导入{Link};
从“./side bar items.css”导入样式;
从'react bootstrap/lib'导入{Navbar、Nav、NavItem、Collapse}
从“react FontAwesome”导入FontAwesome;
类SideBarItems扩展组件{
建造师(道具){
超级(道具);
this.state={open:false};
}
render(){
返回(
仪表板
模块
编程三
)
}
showModules=()=>{
this.setState({open:!this.state.open})
}
}
导出默认值(SideBarItems);
这是我导入图像的地方:

从“React”导入React,{Component}; 从“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);
类UserImage扩展组件{ render(){ 返回( 你好,丹尼尔 软件工程委员会 ) } } 导出默认值(UserImage); 这就是我点击链接时网站的外观


要确保映像是从服务器的根目录而不是当前路由的相对目录获取的,请在
src
前面添加一个
/


也许您可以在html文件中设置基本元素

<base href="http://www.example.com/page.html">

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

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


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

您应该在public/index.html中添加,以便可以从基本服务器而不是相对路径加载图像。

您可以尝试一下吗
?Yepp工作正常。谢谢很好,我会准备一个答案,这样可以帮助别人。不适合我。我正在ES5版本的
cordova
中使用
react js
,别忘了将图像放在公用文件夹下:)。在我意识到它们应该在public.Genius下之前,我把我的文件放在src/下的一个静态文件夹中。给这个小费!这应该是新的被接受的答案,因为它在全球范围内适用于该项目。省去了改变路线的需要,这是迄今为止最优雅的解决方案