欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

react實(shí)現(xiàn)移動(dòng)端二級(jí)路由嵌套詳解

 更新時(shí)間:2022年08月15日 10:07:46   作者:是張魚(yú)小丸子鴨  
這篇文章主要介紹了react移動(dòng)端二級(jí)路由嵌套的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

頁(yè)面效果展示

功能需求

根據(jù)下面不同的標(biāo)題切換不同的頁(yè)面,請(qǐng)求接口數(shù)據(jù),渲染頁(yè)面數(shù)據(jù),點(diǎn)擊左側(cè)數(shù)據(jù),進(jìn)入詳情頁(yè)面,在右側(cè)圖片中點(diǎn)擊返回返回左面頁(yè)面

實(shí)現(xiàn)代碼

我們用到了react中的router,首先我們要下載react的路由,命令是

react-router-dom@5 --save

路由5版本跟6版本使用語(yǔ)法上略有區(qū)別,現(xiàn)在使用較多的是5版本

我們首先在index.js文件中引入react路由,然后進(jìn)行路由跳轉(zhuǎn)

import { default as React } from 'react';
import ReactDOM from 'react-dom/client';
import { HashRouter, Route, Switch } from 'react-router-dom';
import App from './App';
import Detail from './Component/Detail';
import './index.css';
import reportWebVitals from './reportWebVitals';
const root = ReactDOM.createRoot(document.getElementById('root'));
export default function Routers() {
  return (
    <div>
      <HashRouter>
        <Switch>
          <Route path='/' component={App}></Route>
          <Route path='/detil' component={Detail}></Route>
        </Switch>
      </HashRouter>
    </div>
  )
}
root.render(
  <Routers />
);
reportWebVitals();

這樣默認(rèn)打開(kāi)的就是我們的app組件,一定要使用Switch包裹,否則他就是路由模糊匹配,如果是模糊匹配,他可能會(huì)把router路由全部渲染到頁(yè)面,使用Switch他會(huì)從上往下匹配,匹配到一個(gè)路由地址以后就不在繼續(xù)執(zhí)行了

app.js組件

import React, { Component } from 'react';
import { NavLink, Route } from 'react-router-dom';
import './App.css';
import Article from './Component/Article';
import Cart from './Component/Cart';
import Detail from './Component/Detail';
import Home from './Component/Home';
import My from './Component/My';
export default class App extends Component {
  render() {
    return (
      <div className="box">
      {/* 定義二級(jí)路由的地址 */}
      <Route path="/home" component={Home}></Route>
      <Route path="/article" component={Article}></Route>
      <Route path="/cart" component={Cart}></Route>
      <Route path="/my" component={My}></Route>
      <Route path="/detail/:id" component={Detail}></Route>
      {/* 底部導(dǎo)航欄 */}
      <nav>
        <NavLink to="/home" activeClassName="act">
          <div className='title'>首頁(yè)</div>
        </NavLink>
        <NavLink to="/article" activeClassName="act">
          <div className='title'>文章</div>
        </NavLink>
        <NavLink to="/cart" activeClassName="act">
          <div className='title'>購(gòu)物車</div>
        </NavLink>
        <NavLink to="/my" activeClassName="act">
          <div className='title'>我的</div>
        </NavLink>
      </nav>
    </div>
    )
  }
}

app.js組件中有四個(gè)子路由,聲明式-視圖導(dǎo)航 NavLink Link NavLink是Link的包裝,NavLink activeStyle 高亮內(nèi)置樣式 activeClassname設(shè)置高亮class類

Article.js

import axios from 'axios';
import React, { Component } from 'react';
export default class Article extends Component {
    constructor() {
        super();
        this.state = {
            list:[],
        }
        this.getList();
    }
    goDetail = (id)=>{
        this.props.history.push("/detail/"+id);
    }
    //定義獲取文章列表的方法
    getList = async ()=>{
        let {data} = await axios.get("https://api.it120.cc/small4/cms/news/list");
        console.log(data);
        this.setState({
            list:data.data,
        })
    }
  render() {
        let {list} = this.state;
        return (
            <div className='article'>
                <div className="list">
                    {
                        list.map((item, index) => {
                            return (
                                <div className="item" key={index}>
                                    <img src={item.pic} onClick={()=>this.goDetail(item.id)}/>
                                    <div className="title">{item.title}</div>
                                    <button className='del'>刪除</button>
                                </div>
                            )
                        })
                    }
                </div>
            </div>
        )
  }
}

在這點(diǎn)擊圖片跳轉(zhuǎn)到詳情頁(yè)面Detail.js文件

Detail.js

import axios from "axios";
import React, { Component } from 'react';
import NavBar from "./NavBar";
export default class Detail extends Component {
    constructor(props){
        super(props)
        this.state = {
            info:{},
        }
        this.getInfo();
    }
    getInfo = async ()=>{
        let {id} = this.props.match.params;
        console.log(id);
        let {data} = await axios.get("https://api.it120.cc/small4/cms/news/detail?id="+id);
        console.log(data);
        this.setState({
            info:data.data,
        })
    }
  render() {
    let {info} = this.state;
    return (
        <div style={{padding:"10px"}}>
        <NavBar/>
        <h2>{info.title}</h2>
        <img src={info.pic} style={{width:"100%"}}/>
        <div className="info" dangerouslySetInnerHTML={{__html:info.content}}>
        </div>
    </div>
    )
  }
}

在這個(gè)組件中我們封裝了一個(gè)子組件,里面有一個(gè)返回按鈕

import React, { Component } from 'react';
import { withRouter } from 'react-router-dom';
class NavBar extends Component {
    constructor(props){
        super(props)
        console.log(this.props);
    }
  render() {
    return (
      <div className='NavBar'>
        <span onClick={()=>this.props.history.goBack()}>返回</span>
      </div>
    )
  }
}
export default  withRouter(NavBar)

不是所有組件都直接與路由相連(比如拆分的子組件)的,當(dāng)這些組件需要路由參數(shù)時(shí),使用withRouter就可以給此組件傳入路由參數(shù),將react-router的history、location、match三個(gè)對(duì)象傳入props對(duì)象上,此時(shí)就可以使用this.props。

到此這篇關(guān)于react實(shí)現(xiàn)移動(dòng)端二級(jí)路由嵌套詳解的文章就介紹到這了,更多相關(guān)react二級(jí)路由嵌套內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • React Native 截屏組件的示例代碼

    React Native 截屏組件的示例代碼

    本篇文章主要介紹了React Native 截屏組件的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • react 應(yīng)用多入口配置及實(shí)踐總結(jié)

    react 應(yīng)用多入口配置及實(shí)踐總結(jié)

    這篇文章主要介紹了react 應(yīng)用多入口配置及實(shí)踐總結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • react如何實(shí)現(xiàn)表格多條件搜索

    react如何實(shí)現(xiàn)表格多條件搜索

    這篇文章主要介紹了react如何實(shí)現(xiàn)表格多條件搜索問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • react使用節(jié)流函數(shù)防止重復(fù)點(diǎn)擊問(wèn)題

    react使用節(jié)流函數(shù)防止重復(fù)點(diǎn)擊問(wèn)題

    這篇文章主要介紹了react使用節(jié)流函數(shù)防止重復(fù)點(diǎn)擊問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • react map使用方法實(shí)例詳解

    react map使用方法實(shí)例詳解

    map()方法是在React中常用的數(shù)組處理方法之一,可以用于遍歷數(shù)組、生成組件列表以及進(jìn)行數(shù)據(jù)轉(zhuǎn)換等操作,通過(guò)合理運(yùn)用map()方法,可以更靈活地處理和展示數(shù)據(jù),下面給大家講解react map使用方法,感興趣的朋友一起看看吧
    2023-10-10
  • React錯(cuò)誤邊界Error Boundaries

    React錯(cuò)誤邊界Error Boundaries

    錯(cuò)誤邊界是一種React組件,這種組件可以捕獲發(fā)生在其子組件樹(shù)任何位置的JavaScript錯(cuò)誤,并打印這些錯(cuò)誤,同時(shí)展示降級(jí)UI,而并不會(huì)渲染那些發(fā)生崩潰的子組件樹(shù)
    2023-01-01
  • react-router browserHistory刷新頁(yè)面404問(wèn)題解決方法

    react-router browserHistory刷新頁(yè)面404問(wèn)題解決方法

    本篇文章主要介紹了react-router browserHistory刷新頁(yè)面404問(wèn)題解決方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-12-12
  • redux功能強(qiáng)大的Middleware中間件使用學(xué)習(xí)

    redux功能強(qiáng)大的Middleware中間件使用學(xué)習(xí)

    這篇文章主要為大家介紹了redux功能強(qiáng)大的Middleware中間件使用學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • React 添加引用路徑時(shí)如何使用@符號(hào)作為src文件

    React 添加引用路徑時(shí)如何使用@符號(hào)作為src文件

    這篇文章主要介紹了React 添加引用路徑時(shí)如何使用@符號(hào)作為src文件,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-06-06
  • 使用ReactJS實(shí)現(xiàn)tab頁(yè)切換、菜單欄切換、手風(fēng)琴切換和進(jìn)度條效果

    使用ReactJS實(shí)現(xiàn)tab頁(yè)切換、菜單欄切換、手風(fēng)琴切換和進(jìn)度條效果

    這篇文章主要介紹了使用ReactJS實(shí)現(xiàn)tab頁(yè)切換、菜單欄切換、手風(fēng)琴切換和進(jìn)度條效果的相關(guān)資料,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2016-10-10

最新評(píng)論