條件渲染
If 區塊
要有條件地渲染一些標記,我們將其包裝在 if
區塊中:
- if
- if - else
- if let
- if let else
use yew::prelude::*;
html! {
if true {
<p>{ "True case" }</p>
}
};
use yew::prelude::*;
let some_condition = true;
html! {
if some_condition {
<p>{ "True case" }</p>
} else {
<p>{ "False case" }</p>
}
};
use yew::prelude::*;
let some_text = Some("text");
html! {
if let Some(text) = some_text {
<p>{ text }</p>
}
};
use yew::prelude::*;
let some_text = Some("text");
html! {
if let Some(text) = some_text {
<p>{ text }</p>
} else {
<p>{ "False case" }</p>
}
};