v0.6.22

Conditional Rendering

Last updated


This page covers writing :if, :else-if and :else, the comparison operators available, and how conditions are evaluated.

Basic Usage


  1. <div id="app">
  2. <p :if="status == active">Active</p>
  3. <p :else-if="status == pending">Pending</p>
  4. <p :else>Disabled</p>
  5. </div>
  6. <script>
  7. const app = new QUI({ id: "app", data: { status: "active" } });
  8. </script>
Live preview · @pardnchiu/quickui@0.6.22

DirectiveDescription
:ifStarts a condition group
:else-if / :elifMust directly follow the previous condition element at the same level
:elseShown when no earlier condition holds

Only the first element whose condition holds is kept. When none holds and there is no :else, the whole group is removed. An :else-if or :else with no preceding :if is treated as a normal element and always shown.

Comparison Operators


OperatorComparisonExample
NoneTruthiness of the value:if="isLogin"
== / ===Equal after converting to strings:if="status == active"
!= / !==Not equal after converting to strings:if="status != disabled"
> / <Numeric comparison:if="count > 0"
>= / <=Numeric comparison:if="count <= 100"

Special Comparison Values


ValueHolds with == whenHolds with != when
nullThe value is null or missingThe value exists
trueThe value is truthyThe value is falsy
falseThe value is falsyThe value is truthy
emptyThe value is an empty string or missingThe value is not empty

Evaluation Rules


RuleDescription
Right-hand sideAlways a literal, never looked up in data; count > max compares against "max", which becomes 0
Negation!flag is not supported; write flag == false
Compound conditions&& and `` are not supported; nest elements or precompute a boolean in data
Combined with :for:for expands first, then each item evaluates :if on its own and can use the item variable

  1. <ul id="app">
  2. <li :for="user in users" :if="user.active == true">{{ user.name }}</li>
  3. </ul>
  4. <script>
  5. const app = new QUI({
  6. id: "app",
  7. data: {
  8. users: [
  9. { name: "Pardn", active: true },
  10. { name: "Chiu", active: false },
  11. ],
  12. },
  13. });
  14. </script>
Live preview · @pardnchiu/quickui@0.6.22
中文