响应式
更新状态
模板语法
样式
DOM循环
点击事件
DOM引用
条件渲染
Aurelia 2
<button click.trigger="nextLight()">下一个灯</button>
<p>现在亮着的是: ${light}</p>
<p switch.bind="light">
你应该
<span case="红灯">停下</span>
<span case="黄灯">慢行</span>
<span case="绿灯">赶紧走</span>
</p>
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>
生命周期
组件加载时
组件组合
组件Props传参
向父组件emit事件
Aurelia 2
<p>你快乐吗?</p>
<answer-button action-handler.bind="handleAnswer"></answer-button>
<p style="font-size: 50px">${isHappy ? "😀" : "😥"}</p>
slot插槽内容
上下文
Aurelia 2
<h1>欢迎回来, {{ user.username }}</h1>
<user-profile />
<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
<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>
选择器
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>
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应用特性
渲染应用
fetch获取数据
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>
<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>