我知道有些帖子有类似的问题,但是不同的方法似乎难以理解。我想学习更多,并能够使用React.js和Laravel开发多个应用程序。
Header.js:导入文件
import React, { Component } from 'react';
import {
BrowserRouter as Router,
Link,
Switch,
Route
} from 'react-router-dom';
import Home from './Home';
import About from './About';
import Dashboard from '../admins/Dashboard';
import Contact from './Contact';
路由器(BrowserRouter)
<Router>
<div>
<nav className="navbar navbar-expand-lg navbar-dark bg-dark navbar-fixed-top">
<button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav mr-auto">
<li className="nav-item active">
<Link className="nav-link" to="/" exact
>Home</Link>
</li>
<li className="nav-item">
<Link className="nav-link" to="/about">About Us</Link>
</li>
<li className="nav-item">
<Link className="nav-link" to="/contact">Contact</Link>
</li>
</ul>
<form className="form-inline my-2 my-lg-0">
<input className="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search" />
<button className="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
<Switch>
<Route exact path='/' component={Home}/>
<Route exact path="/about" component={About}/>
<Route path='/contact' component={Contact}/>
<Route path='/admins/dashboard' component={Dashboard}/>
</Switch>
</div>
</Router>
当我单击链接时,它会显示链接,但是每当我尝试刷新页面时,它就会显示错误。我尝试了可能的示例,但似乎没有任何效果。我不知道我做错了什么。我一直在尝试看看如何解决。我碰到一篇文章,说createBrowserHistory已被弃用,而React-router v4不支持它,即使我在努力实现该示例也是如此。有更好的办法来解决这个问题吗?
我有多个页面(组件)。
Index.js
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import {
BrowserRouter as Router,
} from 'react-router-dom';
import Header from './Header';
import Footer from './Footer';
export default class Index extends Component {
render() {
return (
<div>
<Header />
<Footer/>
</div>
);
}
}
if (document.getElementById('app')) {
ReactDOM.render(<Index />, document.getElementById('app'));
}
about.js
import React, { Component }from 'react';
import { Redirect } from 'react-router-dom';
class About extends Component {
constructor(props)
{
super(props);
}
render(){
return (
<div className="jumbotron">
<h2>About Us</h2>
</div>
);
}
}
export default About;
app.js
/**
* First we will load all of this project's JavaScript dependencies which
* includes React and other helpers. It's a great starting point while
* building robust, powerful web applications using React + Laravel.
*/
require('./bootstrap');
/**
* Next, we will create a fresh React component instance and attach it to
* the page. Then, you may begin adding components to this application
* or customize the JavaScript scaffolding to fit your unique needs.
*/
require('./components/Index');
答案 0 :(得分:3)
在
上设置basename属性<Router basename={'/directory-name'}>
<Route path='/' component={Home} />
</Router>
如下定义您的幼虫路线
Route::view('/{path?}', 'welcome');
答案 1 :(得分:1)
为了知识起见,我认为您在整个事情上做错了。您的index.js需要从 由于这是Laravel Mix,预设了reactjs,请转到您的路由下的web.php更改为 Route :: view('/ {path?}','welcome'); 这意味着您将视图限制为页面,但是从视图中渲染了多个组件。 在您的welcome.blade.php中,请确保避免使用绝对路径,而应使用类似以下内容的方法 href =“ {{asset('css / app.css')}}” 在您的Header.js中,删除不必要的导入文件。 从“ react-router-dom”导入{Link}; 如果您在了解安排方面遇到任何挑战,可以发送完整的header.js,welcome.blade.php等来寻求帮助import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter, Route, Switch } from 'react-router-dom'
import Header from './Header';
import Footer from './Footer';
import Home from './Home';
import About from './About';
import Dashboard from '../admins/Dashboard';
import Contact from './Contact';
export default class Index extends Component {
render() {
return (
<BrowserRouter>
<div>
<Header/>
<Switch>
<Route exact path='/' component={Home}/>
<Route exact path='/about' component={About}/>
<Route path='/contact' component={Contact}/>
<Route path='/admins/dashboard' component={Dashboard}/>
</Switch>
<Footer/>
</div>
</BrowserRouter>
);
}
}
if (document.getElementById('app')) {
ReactDOM.render(<Index />, document.getElementById('app'));
}
答案 2 :(得分:1)
这对我有帮助:
在你的路由/web.php
Route::get("{path?}", "WebControllers\HomeController@index")->where('path', '.+');
答案 3 :(得分:1)
这种方式拯救了我的一天!
Route::view('{path}', 'Home')->where('path', '([A-z\d\-\/_.]+)?');
答案 4 :(得分:0)
如果你使用 React with Laravel 那么你可以在 Laravel web.php[router file] 上使用它
Route::view('/{path?}', 'welcome');