响应式

声明状态

Marko 标志 Marko

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

Aurelia 1 标志 Aurelia 1

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

更新状态

Marko 标志 Marko

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

Aurelia 1 标志 Aurelia 1

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

计算属性

Marko 标志 Marko

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

Aurelia 1 标志 Aurelia 1

<template>
  <div>${doubleCount}</div>
</template>

模板语法

最简模板

Marko 标志 Marko

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

Aurelia 1 标志 Aurelia 1

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

样式

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>

Aurelia 1 标志 Aurelia 1

.title {
  color: red;
}

DOM循环

Marko 标志 Marko

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

Aurelia 1 标志 Aurelia 1

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

点击事件

Marko 标志 Marko

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

Aurelia 1 标志 Aurelia 1

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

DOM引用

Marko 标志 Marko

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

Aurelia 1 标志 Aurelia 1

<template>
  <input ref="inputElement" />
</template>

条件渲染

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 1 标志 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>

生命周期

组件加载时

Marko 标志 Marko

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

Aurelia 1 标志 Aurelia 1

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

组件卸载时

Marko 标志 Marko

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

Aurelia 1 标志 Aurelia 1

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

组件组合

组件Props传参

Marko 标志 Marko

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

Aurelia 1 标志 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事件

Marko 标志 Marko

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

Aurelia 1 标志 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>

slot插槽

Marko 标志 Marko

<FunnyButton>点我!</FunnyButton>

Aurelia 1 标志 Aurelia 1

<template>
  <require from="./funny-button"></require>

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

slot插槽内容

Marko 标志 Marko

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

Aurelia 1 标志 Aurelia 1

<template>
  <require from="./funny-button"></require>

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

上下文

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 1 标志 Aurelia 1

缺少代码片段 帮助我们改进 Component Party Component Party 标志

表单输入

输入框

Marko 标志 Marko

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

Aurelia 1 标志 Aurelia 1

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

复选框

Marko 标志 Marko

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

Aurelia 1 标志 Aurelia 1

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

单选框

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 1 标志 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>

选择器

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>

Aurelia 1 标志 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>

Web应用特性

渲染应用

Marko 标志 Marko

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

Aurelia 1 标志 Aurelia 1

缺少代码片段 帮助我们改进 Component Party Component Party 标志

fetch获取数据

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>

Aurelia 1 标志 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>