網站首頁 編程語言 正文
react 頁面加載完成后自動執行標簽的點擊事件
當剛進入頁面時react并沒有自動加載路由,因為沒有設置只是設置了key,所以并沒有加載 用到了ant design的layout組件,代碼設置如下:
第一種操作dom的方法:
但是想讓頁面加載完成后自動點擊link這個標簽,然后加載home的組件,方法有這么幾種:
componentDidMount() { if (window.location.hash.split('/')[1]==undefined||window.location.hash.split('/')[1]==""){ // 第一種通過DOM元素獲取標簽并執行點擊事件 var label = document.getElementById("moren").click(); } }
第二種修改window.location.href代碼如下:
componentWillMount() { // 先判斷如果確實是第一次進來就修改其屬性 if (window.location.hash.split('/')[1]==undefined||window.location.hash.split('/')[1]==""){ window.location.href = window.location.href+"#/home" } }
下面看下react 點擊事件自動執行的解決方法?
點擊事件內容若是直接寫Function()
,就會變成執行函數而非事件綁定,頁面加載時會自動執行
class Title extends React.Component { render(){ return <h1 onClick={func('頁面跳轉')}>{title}</h1> } }
用箭頭函數裝飾一下完成間接綁定即可避免:
class Title extends React.Component { render(){ return <h1 onClick={() => func('頁面跳轉')}>{title}</h1> } }
或是:
class Title extends React.Component { render(){ return <h1 onClick={func.bind(this, '頁面跳轉')}>{title}</h1> } }
over
原文鏈接:https://blog.csdn.net/jjw_zyfx/article/details/90033844
相關推薦
- 2022-09-26 使用JDBC連接數據庫執行sql語句,創建數據庫連接池
- 2022-06-15 python?OpenCV?圖像通道數判斷_python
- 2022-08-18 C/C++?extern和static的使用詳解_C 語言
- 2022-08-18 Qt常用容器類的使用_C 語言
- 2022-08-04 Go?slice切片使用示例詳解_Golang
- 2022-05-06 resty更新header控制api版本數據源讀寫分離_其它綜合
- 2022-07-28 C#向數據庫中插入或更新null空值與延遲加載lazy_C#教程
- 2022-10-12 pandas學習之df.fillna的具體使用_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支