代码涉及的主要文件有:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="%PUBLIC_URL%/css/bootstrap.css">
<title>React Apptitle>
head>
<body>
<noscript>You need to enable JavaScript to run this app.noscript>
<div id="root">div>
body>
html>
import React from 'react';
import ReactDOM from 'react-dom';
import {BrowserRouter} from "react-router-dom";
import App from './App';
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
document.getElementById("root")
);
import React, { Component } from 'react'
import {NavLink,Switch,Route,Redirect} from "react-router-dom";
import About from "./pages/About";
import Home from "./pages/Home";
import Header from "./components/Header";
export default class App extends Component {
render() {
return (
<div>
<Header/>
<div className="row">
<div className="col-xs-2 col-xs-offset-2">
<div className="list-group">
<NavLink className="list-group-item" activeClassName='active' to="/home">Home</NavLink>
<NavLink className="list-group-item" activeClassName='active' to="/about">About</NavLink>
</div>
</div>
<div className="col-xs-6">
<div className="panel">
<div className="panel-body">
<Switch>
<Route path="/about" component={About}></Route>
<Route path="/home" component={Home}></Route>
<Redirect to="/home"/>
</Switch>
</div>
</div>
</div>
</div>
</div>
)
}
}
import React, { Component } from 'react'
export default class Header extends Component {
render() {
return (
<div className="row">
<div className="col-xs-offset-2 col-xs-8">
<div className="page-header"><h2>Vue Router Demo</h2></div>
</div>
</div>
)
}
}
import React, { Component } from 'react'
export default class About extends Component {
render() {
return (
<h2>我是About的内容</h2>
)
}
}
import React, { Component } from 'react'
import { NavLink,Switch,Route,Redirect } from 'react-router-dom'
import News from "./News";
import Message from "./Message"
export default class Home extends Component {
render() {
return (
<div>
<h2>Home组件内容</h2>
<div>
<ul className="nav nav-tabs">
<li>
<NavLink to="/home/news" className="list-group-item" activeClassName="active">News</NavLink>
</li>
<li>
<NavLink to="/home/message" className="list-group-item" activeClassName="active">Message</NavLink>
</li>
</ul>
<Switch>
<Route path="/home/news" component={News}></Route>
<Route path="/home/message" component={Message}></Route>
<Redirect to="/home/news"/>
</Switch>
</div>
</div>
)
}
}
import React, { Component } from 'react'
export default class News extends Component {
render() {
return (
<ul>
<li>news001</li>
<li>news002</li>
<li>news003</li>
</ul>
)
}
}
import React, { Component } from 'react'
export default class index extends Component {
render() {
return (
<div>
<ul>
<li>
<a href="/message1">message001</a>
</li>
<li>
<a href="/message2">message002</a>
</li>
<li>
<a href="/message/3">message003</a>
</li>
</ul>
</div>
)
}
}
实现需求:点击message001,详情中显示其ID、TITLE、CONTENT。
因为详情(Detail组件)是一个一般组件,故将其放在components文件夹下,即components文件夹下新建:Detail/index.jsx。
代码变更涉及的文件有:
import React, { Component } from 'react'
const contentArr = [
{id:"001",content:"中国消费韧性强潜力足特点没改变"},
{id:"002",content:"中国队正式递补东京奥运接力铜牌"},
{id:"003",content:"中国人口增长率创57年来最大降幅"},
]
export default class Detail extends Component {
render() {
return (
<ul>
<li>ID:xxx</li>
<li>TITLE:xxx</li>
<li>CONTENT:xxx</li>
</ul>
)
}
}
import React, { Component } from 'react'
import {Link,Route} from "react-router-dom";
import Detail from "../../../components/Detail";
export default class index extends Component {
state = {
messageArr:[
{id:"001",title:"message01"},
{id:"002",title:"message02"},
{id:"003",title:"message03"}
]
}
render() {
const {messageArr} = this.state;
return (
<div>
<ul>
{
messageArr.map(messageObj => {
return (
<li key={messageObj.id}>
<Link to="/home/message/detail">{messageObj.title}</Link>
</li>
)
})
}
</ul>
<hr />
<Route path="/home/message/detail" component={Detail} />
</div>
)
}
}
import React, { Component } from 'react'
const contentArr = [
{id:"001",content:"中国消费韧性强潜力足特点没改变"},
{id:"002",content:"中国队正式递补东京奥运接力铜牌"},
{id:"003",content:"中国人口增长率创57年来最大降幅"},
]
export default class Detail extends Component {
render() {
// 接收params参数
const {params:{id,title}} = this.props.match;
const contentObjMatched = contentArr.find(contentObj => contentObj.id === id);
return (
<ul>
<li>ID:{id}</li>
<li>TITLE:{title}</li>
<li>CONTENT:{contentObjMatched.content}</li>
</ul>
)
}
}
import React, { Component } from 'react'
import {Link,Route} from "react-router-dom";
import Detail from "../../../components/Detail";
export default class index extends Component {
state = {
messageArr:[
{id:"001",title:"message01"},
{id:"002",title:"message02"},
{id:"003",title:"message03"}
]
}
render() {
const {messageArr} = this.state;
return (
<div>
<ul>
{
messageArr.map(messageObj => {
return (
<li key={messageObj.id}>
{/* 传递params参数 */}
<Link to={`/home/message/detail/${messageObj.id}/${messageObj.title}`}>{messageObj.title}</Link>
</li>
)
})
}
</ul>
<hr />
{/* 声明接收params参数 */}
<Route path="/home/message/detail/:id/:title" component={Detail} />
</div>
)
}
}