我正在尝试使用 ReactJS 构建一个 Laravel 项目。我已经在表格上显示了数据。但是,当我添加一些链接(例如创建、编辑、删除...)时,出现了一些错误:
- Failed context type: The context `router` is marked as required in `Link`, but its value is `undefined`.
- Uncaught Error: You should not use `Link` outside a `Router`
Run Code Online (Sandbox Code Playgroud)
所以:
我是新人,所以请帮助我。谢谢。这是我的代码:
示例.js
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import axios from 'axios';
import {Link} from "react-router-dom";
import CreatePost from './CreatePost';
import RoutePath from '../routes/RoutePath';
...
postRow(p){
return (
<tr key = {p.id}>
<td>{p.title}</td>
<td>{p.description}</td>
<td><Link to='/edit'></Link></td>
<td><Link to='/delete'></Link></td>
</tr>
)
}
render() {
const posts = this.state.posts.map(post => this.postRow(post));
return (
<div>
<table border="1">
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
{ posts }
</tbody>
</table>
<div>
<Link to='/add-post'>Add</Link>
</div>
</div>
);
}
}
ReactDOM.render(
<Example />
, document.getElementById('homepage')
);
Run Code Online (Sandbox Code Playgroud)
路由路径.js
import React, {Component} from 'react';
import { Route, BrowserRouter } from "react-router-dom";
import CreatePost from '../components/CreatePost';
import Example from '../components/Example';
export default class RoutePath extends Component{
render(){
return(
<BrowserRouter>
<div>
<Route exact path='/' component={Example}/>
<Route path='/add-post' component={CreatePost}/>
<Route path='/edit' component={Edit}/>
</div>
</BrowserRouter>
)
}
}
Run Code Online (Sandbox Code Playgroud)
这对我有用...我的单页应用程序的单一视图是并且我在我的中home.blade.php
使用BrowserRouter
App.js
Route::get('/', function () {
return view('home');
});
Route::get('/{route}',function(){
return view('home');
});
Run Code Online (Sandbox Code Playgroud)
这将允许您访问http://localhost/并获取应用程序的主页或http://localhost/about-us并获取“about-us”路由。
这是比使用 HashRouter 更好的方法,后者看起来有点难看:http://localhost/#/about-us
归档时间: |
|
查看次数: |
4603 次 |
最近记录: |