gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 05:41:37 24 4
gpt4 key购买 nike

我是第一次使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com