網(wǎng)站首頁 編程語言 正文
react 頁面加載完成后自動執(zhí)行標(biāo)簽的點擊事件
當(dāng)剛進(jìn)入頁面時react并沒有自動加載路由,因為沒有設(shè)置只是設(shè)置了key,所以并沒有加載 用到了ant design的layout組件,代碼設(shè)置如下:
第一種操作dom的方法:
但是想讓頁面加載完成后自動點擊link這個標(biāo)簽,然后加載home的組件,方法有這么幾種:
componentDidMount() { if (window.location.hash.split('/')[1]==undefined||window.location.hash.split('/')[1]==""){ // 第一種通過DOM元素獲取標(biāo)簽并執(zhí)行點擊事件 var label = document.getElementById("moren").click(); } }
第二種修改window.location.href代碼如下:
componentWillMount() { // 先判斷如果確實是第一次進(jìn)來就修改其屬性 if (window.location.hash.split('/')[1]==undefined||window.location.hash.split('/')[1]==""){ window.location.href = window.location.href+"#/home" } }
下面看下react 點擊事件自動執(zhí)行的解決方法?
點擊事件內(nèi)容若是直接寫Function()
,就會變成執(zhí)行函數(shù)而非事件綁定,頁面加載時會自動執(zhí)行
class Title extends React.Component { render(){ return <h1 onClick={func('頁面跳轉(zhuǎn)')}>{title}</h1> } }
用箭頭函數(shù)裝飾一下完成間接綁定即可避免:
class Title extends React.Component { render(){ return <h1 onClick={() => func('頁面跳轉(zhuǎn)')}>{title}</h1> } }
或是:
class Title extends React.Component { render(){ return <h1 onClick={func.bind(this, '頁面跳轉(zhuǎn)')}>{title}</h1> } }
over
原文鏈接:https://blog.csdn.net/jjw_zyfx/article/details/90033844
相關(guān)推薦
- 2022-03-26 C++函數(shù)指針的用法詳解_C 語言
- 2023-02-10 C/C++?extern關(guān)鍵字用法示例全面解析_C 語言
- 2023-03-29 Python中命令行參數(shù)argparse模塊的使用_python
- 2023-01-26 Android?源碼淺析RecyclerView?Adapter_Android
- 2022-07-22 關(guān)于不定方程解的個數(shù)的問題
- 2022-05-08 python如何生成密碼字典_python
- 2022-06-28 Python技法之簡單遞歸下降Parser的實現(xiàn)方法_python
- 2022-11-17 python中的queue隊列類型及函數(shù)用法_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 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錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支