响应式

声明状态

Aurelia 2 标志 Aurelia 2

<h1>你好 ${name}</h1>

Marko 标志 Marko

<let/name="张三">
<h1>你好 ${name}</h1>

更新状态

Aurelia 2 标志 Aurelia 2

<h1>你好 ${name}</h1>

Marko 标志 Marko

<let/name="张三">
<script>name = "李四"</script>
<h1>你好 ${name}</h1>

计算属性

Aurelia 2 标志 Aurelia 2

<div>${doubleCount}</div>

Marko 标志 Marko

<let/count=10>
<const/doubleCount=count * 2>
<div>${doubleCount}</div>

模板语法

最简模板

Aurelia 2 标志 Aurelia 2

<h1>你好,世界</h1>

Marko 标志 Marko

<h1>你好,世界</h1>

样式

Aurelia 2 标志 Aurelia 2

.title {
  color: red;
}

Marko 标志 Marko

<h1.title>我是红色的</h1>
<button style={ "font-size": "10rem" }>我是一个按钮</button>
<button class=scopedButton>我是一个样式作用域按钮</button>

<style>
  .title {
    color: red;
  }
</style>
<style/{ scopedButton }>
  .scopedButton {
    font-size: 10rem;
  }
</style>

DOM循环

Aurelia 2 标志 Aurelia 2

<ul>
  <li repeat.for="color of colors">${color}</li>
</ul>

Marko 标志 Marko

<ul>
  <for|color| of=["红", "绿", "蓝"]>
    <li>${color}</li>
  </for>
</ul>

点击事件

Aurelia 2 标志 Aurelia 2

<p>计数器: ${count}</p>
<button click.trigger="incrementCount">+1</button>

Marko 标志 Marko

<let/count=0>
<p>计数器: ${count}</p>
<button onClick() { count++ }>+1</button>

DOM引用

Aurelia 2 标志 Aurelia 2

<input ref="inputElement" />

Marko 标志 Marko

<input/inputElement>
<script>
  inputElement().focus();
</script>

条件渲染

Aurelia 2 标志 Aurelia 2

<button click.trigger="nextLight()">下一个灯</button>
<p>现在亮着的是: ${light}</p>
<p switch.bind="light">
  你应该
  <span case="红灯">停下</span>
  <span case="黄灯">慢行</span>
  <span case="绿灯">赶紧走</span>
</p>

Marko 标志 Marko

static const TRAFFIC_LIGHTS = ["红灯", "黄灯", "绿灯"];
<let/lightIndex=0>
<const/light=TRAFFIC_LIGHTS[lightIndex]>

<button onClick() { lightIndex = (lightIndex + 1) % TRAFFIC_LIGHTS.length }>
  下一个灯
</button>
<p>现在亮着的是: ${light}</p>
<p>
  你应该
  <if=light === "红灯">停下</if>
  <else if=light === "黄灯">慢行</else>
  <else>赶紧走</else>
</p>

生命周期

组件加载时

Aurelia 2 标志 Aurelia 2

<p>页面标题: ${pageTitle}</p>

Marko 标志 Marko

<let/pageTitle="">
<script>pageTitle = document.title</script>
<p>页面标题: ${pageTitle}</p>

组件卸载时

Aurelia 2 标志 Aurelia 2

<p>当前时间: ${time}</p>

Marko 标志 Marko

<let/time=new Date()>
<script>
  const id = setInterval(() => time = new Date(), 1000);
  $signal.onabort = () => clearInterval(id)
</script>
<p>当前时间: ${time.toLocaleTimeString()}</p>

组件组合

组件Props传参

Aurelia 2 标志 Aurelia 2

<user-profile
  name.bind
  age.bind
  favourite-colors.bind="colors"
  is-available.bind="available"
></user-profile>

Marko 标志 Marko

<UserProfile
  name="张三"
  age=20
  favouriteColors=["green", "blue", "red"]
  isAvailable
/>

向父组件emit事件

Aurelia 2 标志 Aurelia 2

<p>你快乐吗?</p>
<answer-button action-handler.bind="handleAnswer"></answer-button>
<p style="font-size: 50px">${isHappy ? "😀" : "😥"}</p>

Marko 标志 Marko

<let/isHappy=true>
<p>你快乐吗?</p>
<AnswerButton
  onYes() { isHappy = true }
  onNo() { isHappy = false }
/>
<p style={ "font-size": 50 }>${isHappy ? "😀" : "😥"}</p>

slot插槽

Aurelia 2 标志 Aurelia 2

<funny-button>点我!</funny-button>

Marko 标志 Marko

<FunnyButton>点我!</FunnyButton>

slot插槽内容

Aurelia 2 标志 Aurelia 2

<funny-button></funny-button>
<funny-button>这是传到slot的内容!</funny-button>

Marko 标志 Marko

<FunnyButton/>
<FunnyButton>这是传到slot的内容!</FunnyButton>

上下文

Aurelia 2 标志 Aurelia 2

<h1>欢迎回来, {{ user.username }}</h1>
<user-profile />

Marko 标志 Marko

<let/user={
  id: 1,
  username: "abcdefg",
  email: "[email protected]",
}>
<const/updateUsername(newUsername) {
  user = { ...user, username: newUsername };
}>

<h1>欢迎回来, ${user.username}</h1>
<context={ ...user, updateUsername }>
  <UserProfile />
</context>

表单输入

输入框

Aurelia 2 标志 Aurelia 2

<p>${text}</p>
<input value.bind />

Marko 标志 Marko

<let/text="你好世界">
<p>${text}</p>
<input value:=text>

复选框

Aurelia 2 标志 Aurelia 2

<input id="is-available" type="checkbox" checked.bind="isAvailable" />
<label for="is-available">这是一个checkbox</label>: ${isAvailable}

Marko 标志 Marko

<input#is-available
  type="checkbox"
  checked:=input.isAvailable
>
<label for="is-available">这是一个checkbox</label>

单选框

Aurelia 2 标志 Aurelia 2

<div>你选择了: ${picked}</div>

<input id="blue-pill" checked.bind="picked" type="radio" value="blue" />
<label for="blue-pill">蓝色</label>

<input id="red-pill" checked.bind="picked" type="radio" value="red" />
<label for="red-pill">红色</label>

Marko 标志 Marko

<let/picked="red">

<div>你选择了: ${picked}</div>
<input#blue-pill
  type="radio"
  value="blue"
  checkedValue:=picked
>
<label for="blue-pill">蓝色</label>

<input#red-pill
  type="radio"
  value="red"
  checkedValue:=picked
>
<label for="red-pill">红色</label>

选择器

Aurelia 2 标志 Aurelia 2

<select value.bind="selectedColorId">
  <option value="">选择一个颜色</option>
  <option
    repeat.for="color of colors"
    value.bind="color.id"
    disabled.bind="color.isDisabled"
  >
    ${color.text}
  </option>
</select>

Marko 标志 Marko

static const colors = [
  { id: 1, text: "红" },
  { id: 2, text: "蓝" },
  { id: 3, text: "绿" },
  { id: 4, text: "灰", isDisabled: true },
];
<let/selectedColorId=2>

<select value:=selectedColorId>
  <for|{ id, isDisabled, text }| of=colors>
    <option value=id disabled=isDisabled>
      ${text}
    </option>
  </for>
</select>

Web应用特性

渲染应用

Aurelia 2 标志 Aurelia 2

<!doctype html>
<html>
  <head>
    <script type="module" src="./main.ts"></script>
  </head>

  <body>
    <app></app>
  </body>
</html>

Marko 标志 Marko

<!doctype html>
<html>
  <body>
    <div id="app"></div>
    <script type="module" src="./app.js"></script>
  </body>
</html>

fetch获取数据

Aurelia 2 标志 Aurelia 2

<template promise.bind="useFetchUsers.fetchData()">
  <p pending>正在获取用户信息……</p>
  <p catch>获取用户信息出错</p>
  <ul then.from-view="users">
    <li repeat.for="user of users">
      <img src.bind="user.picture.thumbnail" alt="user" />
      <p>${ user.name.first } ${ user.name.last }</p>
    </li>
  </ul>
</template>

Marko 标志 Marko

<try>
  <await|{ results: users }|=fetch("https://randomuser.me/api/?results=3").then(res => res.json())>
    <ul>
      <for|{ picture, name }| of=users>
        <li>
          <img src=picture.thumbnail alt="user">
          <p>${name.first} ${name.last}</p>
        </li>
      </for>
    </ul>
  </await>
  <@placeholder>
    <p>正在获取用户信息……</p>
  </@placeholder>
  <@catch|error|>
    <p>获取用户信息出错</p>
  </@catch>
</try>