元素

标签结构

元素标签必须是自闭合的 <... />,或是每个标签都有一个对应的闭合标签。

  1. html! {
  2. <div id="my_div"></div>
  3. }
  1. html! {
  2. <div id="my_div"> // <- 缺少闭合标签
  3. }
  1. html! {
  2. <input id="my_input" />
  3. }
  1. html! {
  2. <input id="my_input"> // <- 没有自闭合
  3. }

元素 - 图1备注

为方便起见,一些 通常 需要闭合标签的元素是被允许自闭合的。例如,html! { <div class="placeholder" /> } 这样写是有效的。

Children

轻松创建复杂的嵌套 HTML 和 SVG 布局:

  1. html! {
  2. <div>
  3. <div data-key="abc"></div>
  4. <div class="parent">
  5. <span class="child" value="anything"></span>
  6. <label for="first-name">{ "First Name" }</label>
  7. <input type="text" id="first-name" value="placeholder" />
  8. <input type="checkbox" checked=true />
  9. <textarea value="write a story" />
  10. <select name="status">
  11. <option selected=true disabled=false value="">{ "Selected" }</option>
  12. <option selected=false disabled=true value="">{ "Unselected" }</option>
  13. </select>
  14. </div>
  15. </div>
  16. }
  1. html! {
  2. <svg width="149" height="147" viewBox="0 0 149 147" fill="none" xmlns="http://www.w3.org/2000/svg">
  3. <path d="M60.5776 13.8268L51.8673 42.6431L77.7475 37.331L60.5776 13.8268Z" fill="#DEB819"/>
  4. <path d="M108.361 94.9937L138.708 90.686L115.342 69.8642" stroke="black" stroke-width="4" stroke-linecap="round" stroke-linejoin="round"/>
  5. <g filter="url(#filter0_d)">
  6. <circle cx="75.3326" cy="73.4918" r="55" fill="#FDD630"/>
  7. <circle cx="75.3326" cy="73.4918" r="52.5" stroke="black" stroke-width="5"/>
  8. </g>
  9. <circle cx="71" cy="99" r="5" fill="white" fill-opacity="0.75" stroke="black" stroke-width="3"/>
  10. <defs>
  11. <filter id="filter0_d" x="16.3326" y="18.4918" width="118" height="118" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB">
  12. <feGaussianBlur stdDeviation="2"/>
  13. <feColorMatrix in="SourceAlpha" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0"/>
  14. </filter>
  15. </defs>
  16. </svg>
  17. }

Classes

有许多方便的选项可用于元素指定 classes:

  1. html! {
  2. <div class="container"></div>
  3. }
  1. html! {
  2. <div class="container center-align"></div>
  3. }
  1. html! {
  2. <div class=format!("{}-container", size)></div>
  3. }
  1. html! {
  2. <div class=self.classes()></div>
  3. }
  1. html! {
  2. <div class=("class-1", "class-2")></div>
  3. }
  1. html! {
  2. <div class=vec!["class-1", "class-2"]></div>
  3. }

监听器

监听器属性需要传递一个由闭包包裹的 Callback。创建回调的方式取决于你希望你的应用程序如何响应监听器事件:

  1. struct MyComponent {
  2. link: ComponentLink<Self>,
  3. }
  4. enum Msg {
  5. Click,
  6. }
  7. impl Component for MyComponent {
  8. type Message = Msg;
  9. type Properties = ();
  10. fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
  11. MyComponent { link }
  12. }
  13. fn update(&mut self, msg: Self::Message) -> ShouldRender {
  14. match msg {
  15. Msg::Click => {
  16. // 处理 Click
  17. }
  18. }
  19. }
  20. fn view(&self) -> Html {
  21. // 从组件 link 中创建回调来在组件中处理它
  22. let click_callback = self.link.callback(|_: ClickEvent| Msg::Click);
  23. html! {
  24. <button onclick=click_callback>
  25. { "Click me!" }
  26. </button>
  27. }
  28. }
  29. }
  1. struct MyComponent {
  2. worker: Dispatcher<MyWorker>,
  3. }
  4. impl Component for MyComponent {
  5. type Message = ();
  6. type Properties = ();
  7. fn create(_: Self::Properties, _: ComponentLink<Self>) -> Self {
  8. MyComponent {
  9. worker: MyWorker::dispatcher()
  10. }
  11. }
  12. fn update(&mut self, _: Self::Message) -> ShouldRender {
  13. false
  14. }
  15. fn view(&self) -> Html {
  16. // 从 worker 中创建回调来在另一个上下文中处理它
  17. let click_callback = self.worker.callback(|_: ClickEvent| WorkerMsg::Process);
  18. html! {
  19. <button onclick=click_callback>
  20. { "Click me!" }
  21. </button>
  22. }
  23. }
  24. }
  1. struct MyComponent;
  2. impl Component for MyComponent {
  3. type Message = ();
  4. type Properties = ();
  5. fn create(_: Self::Properties, _: ComponentLink<Self>) -> Self {
  6. MyComponent
  7. }
  8. fn update(&mut self, _: Self::Message) -> ShouldRender {
  9. false
  10. }
  11. fn view(&self) -> Html {
  12. // 创建一个短暂的回调
  13. let click_callback = Callback::from(|| {
  14. ConsoleService::log("clicked!");
  15. });
  16. html! {
  17. <button onclick=click_callback>
  18. { "Click me!" }
  19. </button>
  20. }
  21. }
  22. }