網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
1. 發(fā)起網(wǎng)絡(luò)請(qǐng)求
首先需要安裝 axios 庫(kù):
yarn add axios
發(fā)起網(wǎng)絡(luò)請(qǐng)求:
import React, { Component } from 'react' import { get } from './utils/http' import Loading from './components/Loading' class App extends Component { state = { users: null } async componentDidMount() { let users = await get('/mock/users.json') this.setState({ users }) } render() { return ( <div> { // 條件渲染 // 第一次執(zhí)行過來(lái)是null,防止報(bào)錯(cuò)就需要加一個(gè)判斷 this.state.users ? <h3>{this.state.users.name}</h3> : <Loading /> } </div> ) } } export default App
mock 數(shù)據(jù):
{ "id": 1, "name": "張三" }
2. 開發(fā)時(shí)網(wǎng)絡(luò)請(qǐng)求代理配置
使用create-react-app
來(lái)創(chuàng)建的react工程,如果有請(qǐng)求跨域,在開發(fā)時(shí)的解決方案首先需要新建配置文件 src/setupProxy.js
文件,并通過 yarn 安裝 http-proxy-middleware,代理中間件模塊:
yarn add -D http-proxy-middleware
setupProxy.js:
// 此文件是create-react-app創(chuàng)建的工程提供的一個(gè)代理網(wǎng)絡(luò)請(qǐng)求文件入口 // 此文件給nodejs調(diào)用,模塊化要使用commonjs // 修改此文件需要重啟項(xiàng)目 // 實(shí)現(xiàn)代理需要安裝 代理中間件模塊 yarn add -D http-proxy-middleware const { createProxyMiddleware: proxy } = require('http-proxy-middleware') const userMockFn = require('../mock/user') // // 內(nèi)置了express module.exports = app => { userMockFn(app) // 參數(shù)1:以什么規(guī)則開頭 app.use('/api', proxy({ // 目標(biāo)地址 target: 'https://api.iynn.cn/film', // 修改主機(jī)頭 changeOrigin: true })) }
App.jsx:
import React, { Component } from 'react' import { get } from './utils/http' import Loading from './components/Loading' class App extends Component { state = { users: null } async componentDidMount() { let users = await get('/api/v1/getNowPlayingFilmList?cityId=110100&pageNum=1&pageSize=10') console.log(users) } render() { return ( <div> { // 條件渲染 // 第一次執(zhí)行過來(lái)是null,防止報(bào)錯(cuò)就需要加一個(gè)判斷 this.state.users ? <h3>{this.state.users.name}</h3> : <Loading /> } </div> ) } } export default App
原文鏈接:https://blog.csdn.net/weixin_45605541/article/details/127024561
相關(guān)推薦
- 2022-07-13 淺談Redis中的自動(dòng)過期機(jī)制_Redis
- 2022-05-22 vscode調(diào)試container中的程序的方法步驟_相關(guān)技巧
- 2022-05-22 使用VirtualBox和Vagrant搭建Linux環(huán)境的方法步驟_VirtualBox
- 2022-07-01 python讀取nc數(shù)據(jù)并繪圖的方法實(shí)例_python
- 2022-12-06 nginx?pod?hook鉤子優(yōu)雅關(guān)閉示例詳解_nginx
- 2022-08-20 python程序的打包分發(fā)示例詳解_python
- 2022-05-26 C++?棧和隊(duì)列的實(shí)現(xiàn)超詳細(xì)解析_C 語(yǔ)言
- 2022-08-28 C++實(shí)現(xiàn)貪心算法的示例詳解_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支