响应式
声明状态
更新状态
模板语法
最简模板
样式
DOM循环
点击事件
Aurelia 1
<template>
<p>计数器: ${count}</p>
<button click.trigger="incrementCount()">+1</button>
</template>
DOM引用
条件渲染
Aurelia 1
<template>
<button click.trigger="nextLight()">下一个灯</button>
<p>现在亮着的是: ${light}</p>
<p>
你应该
<span if.bind="light === '红灯'">停下</span>
<span if.bind="light === '黄灯'">慢行</span>
<span if.bind="light === '绿灯'">赶紧走</span>
</p>
</template>
import { track } from "ripple";
const TRAFFIC_LIGHTS = ["红灯", "黄灯", "绿灯"];
export default function TrafficLight() {
let &[lightIndex] = track(0);
let &[light] = track(() => TRAFFIC_LIGHTS[lightIndex]);
function nextLight() {
lightIndex = (lightIndex + 1) % TRAFFIC_LIGHTS.length;
}
return <>
<button onClick={nextLight}>"下一个灯"</button>
<p>"现在亮着的是: "{light}</p>
<p>
"你应该 "
if (light === "红灯") {
<span>"停下"</span>
} else if (light === "黄灯") {
<span>"慢行"</span>
} else if (light === "绿灯") {
<span>"赶紧走"</span>
}
</p>
</>;
}
生命周期
组件加载时
组件卸载时
Aurelia 1
<template>
<p>当前时间: ${time}</p>
</template>
import { effect, track } from "ripple";
export default function Time() {
let &[time] = track(new Date().toLocaleTimeString());
effect(() => {
const timer = setInterval(() => {
time = new Date().toLocaleTimeString();
}, 1000);
return () => clearInterval(timer);
});
return <h1>"当前时间: "{time}</h1>;
}
组件组合
组件Props传参
Aurelia 1
<template>
<require from="./user-profile"></require>
<user-profile
name.bind="name"
age.bind="age"
favourite-colors.bind="colors"
is-available.bind="available"
></user-profile>
</template>
向父组件emit事件
Aurelia 1
<template>
<require from="./answer-button"></require>
<p>你快乐吗?</p>
<answer-button action-handler.call="handleAnswer(reply)"></answer-button>
<p style="font-size: 50px">${isHappy ? "😀" : "😥"}</p>
</template>
import { track } from "ripple";
import { AnswerButton } from "./AnswerButton.tsrx";
export function App() {
let &[isHappy] = track(true);
function onAnswerNo() {
isHappy = false;
}
function onAnswerYes() {
isHappy = true;
}
return <>
<p>"你快乐吗?"</p>
<AnswerButton onYes={onAnswerYes} onNo={onAnswerNo} />
<p style={{ fontSize: "50px" }}>{isHappy ? "😀" : "😥"}</p>
</>;
}
slot插槽
slot插槽内容
Aurelia 1
<template>
<require from="./funny-button"></require>
<funny-button></funny-button>
<funny-button>这是传到slot的内容!</funny-button>
</template>
上下文
Aurelia 1
缺少代码片段 帮助我们改进 Component Party 
import { RippleObject } from "ripple";
import { UserContext } from "./UserContext.tsrx";
import { UserProfile } from "./UserProfile.tsrx";
export function App() {
const user = new RippleObject({
id: 1,
username: "abcdefg",
email: "[email protected]",
});
UserContext.set(user);
return <>
<h1>"欢迎回来, "{user.username}</h1>
<UserProfile />
</>;
}
表单输入
输入框
复选框
Aurelia 1
<template>
<input id="is-available" type="checkbox" checked.bind="isAvailable" />
<label for="is-available">这是一个checkbox</label>: ${isAvailable}
</template>
单选框
Aurelia 1
<template>
<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>
</template>
import { bindGroup, track } from "ripple";
export default function PickPill() {
let &[picked, pickedTracked] = track("red");
return <>
<div>"你选择了: "{picked}</div>
<input
id="blue-pill"
type="radio"
value="blue"
ref={bindGroup(pickedTracked)}
/>
<label for="blue-pill">"蓝色"</label>
<input
id="red-pill"
type="radio"
value="red"
ref={bindGroup(pickedTracked)}
/>
<label for="red-pill">"红色"</label>
</>;
}
选择器
Aurelia 1
<template>
<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>
</template>
import { bindValue, track } from "ripple";
const colors = [
{ id: 1, text: "红" },
{ id: 2, text: "蓝" },
{ id: 3, text: "绿" },
{ id: 4, text: "灰", isDisabled: true },
];
export default function ColorSelect() {
let &[selectedColorId, selectedColorIdTracked] = track(2);
return <select ref={bindValue(selectedColorIdTracked)}>
for (const color of colors; key color.id) {
<option value={color.id} disabled={color.isDisabled}>
{color.text}
</option>
}
</select>;
}
Web应用特性
渲染应用
Aurelia 1
缺少代码片段 帮助我们改进 Component Party 
Ripple
<!doctype html>
<html>
<body>
<div id="app"></div>
<script type="module" src="./main.js"></script>
</body>
</html>
fetch获取数据
Aurelia 1
<template>
<p if.bind="isLoading">正在获取用户信息……</p>
<p if.bind="error">获取用户信息出错</p>
<ul if.bind="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>
import { useFetchUsers } from "./useFetchUsers.tsrx";
export function App() {
const data = useFetchUsers();
return <>
if (data.isLoading) {
<p>"正在获取用户信息……"</p>
} else if (data.error) {
<p>"获取用户信息出错"</p>
} else if (data.users) {
<ul>
for (const user of data.users; key user.login.uuid) {
<li>
<img src={user.picture.thumbnail} alt="用户" />
<p>{user.name.first}" "{user.name.last}</p>
</li>
}
</ul>
}
</>;
}