mirror of
https://github.com/yewstack/yew.git
synced 2025-12-08 21:26:25 +00:00
* 0.20 Changelog * Improve changelog generator * Add blog post * Add blog post * Apply suggestions from code review Co-authored-by: WorldSEnder <WorldSEnder@users.noreply.github.com> Co-authored-by: Julius Lungys <32368314+voidpumpkin@users.noreply.github.com> * update Changelog * update Cargo.toml * changelog gen compiles * website version 0.20 * add migration guides * prettier * i18n Co-authored-by: WorldSEnder <WorldSEnder@users.noreply.github.com> Co-authored-by: Julius Lungys <32368314+voidpumpkin@users.noreply.github.com>
27 lines
576 B
Plaintext
27 lines
576 B
Plaintext
---
|
|
description: 外帶 DOM 的存取
|
|
---
|
|
|
|
# Refs
|
|
|
|
## Refs
|
|
|
|
`ref` 關鍵字可以被使用在任何 HTML 的元素或是元件,用來得到那個物件附加的 DOM `Element`。這個可以在 view 生命周期方法之外,改變 DOM。
|
|
|
|
對於要存取 canvas 元素,或滾動到頁面不同的區塊,很有幫助。
|
|
|
|
語法可以這樣使用:
|
|
|
|
```rust
|
|
// 建立
|
|
self.node_ref = NodeRef::default();
|
|
|
|
// 在 view 裡
|
|
html! {
|
|
<div ref={self.node_ref.clone()}></div>
|
|
}
|
|
|
|
// 更新
|
|
let has_attributes = self.node_ref.cast::<Element>().unwrap().has_attributes();
|
|
```
|