Refs

ref 关键词可被用在任何 HTML 元素或组件内部以获得与之相关联的 DOM 元素 。这样就能在 view 生命周期方法之外对 DOM 进行更改。

用这个可以方便地获取 canvas 元素或者滚动到页面的不同部分。

语法如下:

1. // 在 create 中
2. self.node_ref = NodeRef::default();

4. // 在 view 中
5. html! {
6. <div ref=self.node_ref.clone()></div>
7. }

9. // 在 update 中
10. let has_attributes = self.node_ref.cast::<Element>().unwrap().has_attributes();