網(wǎng)站首頁(yè) 編程語言 正文
使用裝飾器調(diào)用
裝飾器 用來裝飾類的,可以增強(qiáng)類,在不修改類的內(nèi)部的源碼的同時(shí),增強(qiáng)它的能力(屬性或方法)
裝飾器使用@函數(shù)名寫法,對(duì)類進(jìn)行裝飾,目前在js中還是提案,使用需要配置相關(guān)兼容代碼庫(kù)。
react腳手架創(chuàng)建的項(xiàng)目默認(rèn)是不支持裝飾器,需要手動(dòng)安裝相關(guān)模塊和添加配置文件
??安裝相關(guān)模塊
yarn add -D customize-cra react-app-rewired ?@babel/plugin-proposal-decorators
??修改package.json文件中scripts命令
"scripts": { "start": "react-app-rewired start", "build": "react-app-rewired build", "test": "react-app-rewired test", "eject": "react-scripts eject" }
??在項(xiàng)目根目錄中添加config-overrides.js
配置文件
此文件可以理解為就是webpack.config.js
的擴(kuò)展文件
const { resolve } = require('path') const { addDecoratorsLegacy, override } = require('customize-cra') // 增強(qiáng)自定義給webpack添加相關(guān)配置 const custom = () => config => { config.resolve.alias['@'] = resolve('./src') return config } module.exports = override(addDecoratorsLegacy(), custom())
一般加上這個(gè)就行:
// 增量配置當(dāng)前項(xiàng)目中的webpack配置,建議在react18中不要用 // 建議在react18中也不要用裝飾器 // override 方法,如果webpack中有此配置則,覆蓋,如果沒有則添加 const { addDecoratorsLegacy, override } = require('customize-cra') // 追加上一個(gè)裝飾器 module.exports = override(addDecoratorsLegacy())
裝飾器的使用
裝飾類
裝飾函數(shù),在裝飾時(shí)它沒有寫小括號(hào),target它就是當(dāng)前你裝飾的類
添加靜態(tài)方法/屬性:
不是成員方法
const handle = target =>{ target.run = ()=>{ console.log("run"); } } @handle class Demo {}; const d = new Demo(); Demo.run();
添加成員方法/屬性:
const handle = target =>{ target.prototype.run = ()=>{ console.log("run"); } } @handle class Demo {}; const d = new Demo(); d.run();
裝飾器加上了小括號(hào),則定義函數(shù)時(shí)一定要返回一個(gè)新函數(shù)
const handle = (msg) => target =>{ target.prototype.run = ()=>{ console.log("run",msg); } } @handle("我愛你") class Demo {}; const d = new Demo(); d.run();
裝飾成員屬性
裝飾器裝飾成員屬性,裝飾時(shí)沒有寫小括號(hào)
裝飾器裝飾成員屬性,裝飾時(shí)沒有寫小括號(hào) target 裝飾的類的實(shí)例 new Demo this key 當(dāng)前的成員屬性名稱 username description 就是當(dāng)前屬性在對(duì)象中它的描述 Object.defineProperty const handle = (target, key, description) => { // 設(shè)置當(dāng)前屬性為只讀屬性 // description.writable = false console.log(target, key, description) } class Demo { @handle username = 'abc' }
高階組件
定義高階組件,用來進(jìn)行全局布局
1.它就是一個(gè)函數(shù)
2.參數(shù)首字母大寫,因?yàn)槟銈魅氲氖且粋€(gè)組件,在react中組件的調(diào)用必須首字母大寫
3.返回一個(gè)組件
簡(jiǎn)單的樣式修改
一、沒有使用cssModule時(shí),只能直接導(dǎo)入
import './style.css' <div className="todo-title">任務(wù)列表</div>
注意手動(dòng)添加標(biāo)識(shí)符
例如:
.todo-title { color: red; }
二、 在react中create-react-app工程,它內(nèi)置了cssModule功能,但是文件后綴一定要有xxx.module.css/scss
如果有scss文件,必須得安裝yarn add sass
import styles from './style.module.css'
引用:
<div className={styles.title}>任務(wù)列表</div> <span className={done ? style.title : ''}>{title}</span>
原文鏈接:https://niuniu.blog.csdn.net/article/details/126984533
相關(guān)推薦
- 2022-12-06 react?fiber執(zhí)行原理示例解析_React
- 2022-08-30 啟動(dòng)Activity但是不顯示界面
- 2022-11-12 Python創(chuàng)建SQL數(shù)據(jù)庫(kù)流程逐步講解_python
- 2022-06-22 Python實(shí)現(xiàn)npy/mat文件的保存與讀取_python
- 2022-05-25 Properties與ResourceBundle的基本使用以及區(qū)別
- 2022-09-12 C語言多媒體框架GStreamer使用教程深講_C 語言
- 2022-08-13 采用python開發(fā)sparkstreming全流程
- 2022-09-17 ASP.NET?Core實(shí)現(xiàn)AES-GCM加密算法_實(shí)用技巧
- 最近更新
-
- 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)程分支