响应式
声明状态
更新状态
Vue 2
<script>
export default {
data() {
return {
name: "张三",
};
},
created() {
this.name = "李四";
},
};
</script>
<template>
<h1>你好 {{ name }}</h1>
</template>
计算属性
Vue 2
<script>
export default {
data() {
return {
count: 10,
};
},
computed: {
doubleCount() {
return this.count * 2;
},
},
};
</script>
<template>
<div>{{ doubleCount }}</div>
</template>
模板语法
最简模板
样式
Vue 2
<template>
<div>
<h1 class="title">我是红色的</h1>
<button style="font-size: 10rem">我是一个按钮</button>
</div>
</template>
<style scoped>
.title {
color: red;
}
</style>
DOM循环
Vue 2
<script>
export default {
data() {
return {
colors: ["红", "绿", "蓝"],
};
},
};
</script>
<template>
<ul>
<li v-for="color in colors" :key="color">
{{ color }}
</li>
</ul>
</template>
点击事件
Vue 2
<script>
export default {
data() {
return {
count: 0,
};
},
methods: {
incrementCount() {
this.count++;
},
},
};
</script>
<template>
<div>
<p>计数器: {{ count }}</p>
<button @click="incrementCount">+1</button>
</div>
</template>
DOM引用
条件渲染
Vue 2
<script>
export default {
data() {
return {
TRAFFIC_LIGHTS: ["红灯", "黄灯", "绿灯"],
lightIndex: 0,
};
},
computed: {
light() {
return this.TRAFFIC_LIGHTS[this.lightIndex];
},
},
methods: {
nextLight() {
this.lightIndex = (this.lightIndex + 1) % this.TRAFFIC_LIGHTS.length;
},
},
};
</script>
<template>
<div>
<button @click="nextLight">下一个灯</button>
<p>现在亮着的是: {{ light }}</p>
<p>
你应该
<span v-if="light === '红灯'">停下</span>
<span v-else-if="light === '黄灯'">慢行</span>
<span v-else-if="light === '绿灯'">赶紧走</span>
</p>
</div>
</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>
</>;
}
生命周期
组件加载时
Vue 2
<script>
export default {
data() {
return {
pageTitle: "",
};
},
mounted() {
this.pageTitle = document.title;
},
};
</script>
<template>
<p>页面标题: {{ pageTitle }}</p>
</template>
组件卸载时
Vue 2
<script>
export default {
data() {
return {
time: new Date().toLocaleTimeString(),
timer: null,
};
},
mounted() {
this.timer = setInterval(() => {
this.time = new Date().toLocaleTimeString();
}, 1000);
},
beforeDestroy() {
clearInterval(this.timer);
},
};
</script>
<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传参
Vue 2
<script>
import UserProfile from "./UserProfile.vue";
export default {
components: {
UserProfile,
},
};
</script>
<template>
<UserProfile
name="张三"
:age="20"
:favorite-colors="['green', 'blue', 'red']"
is-available
/>
</template>
向父组件emit事件
Vue 2
<script>
import AnswerButton from "./AnswerButton.vue";
export default {
components: {
AnswerButton,
},
data() {
return {
isHappy: true,
};
},
methods: {
onAnswerNo() {
this.isHappy = false;
},
onAnswerYes() {
this.isHappy = true;
},
},
};
</script>
<template>
<div>
<p>你快乐吗?</p>
<AnswerButton @yes="onAnswerYes" @no="onAnswerNo" />
<p style="font-size: 50px">
{{ isHappy ? "😀" : "😥" }}
</p>
</div>
</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插槽内容
Vue 2
<script>
import FunnyButton from "./FunnyButton.vue";
export default {
components: {
FunnyButton,
},
};
</script>
<template>
<div>
<FunnyButton />
<FunnyButton> 这是传到slot的内容! </FunnyButton>
</div>
</template>
上下文
Vue 2
<script>
import UserProfile from "./UserProfile.vue";
export default {
components: { UserProfile },
provide() {
return {
user: Object.assign(this.user, {
updateUsername: this.updateUsername,
}),
};
},
data() {
return {
user: {
id: 1,
username: "abcdefg",
email: "[email protected]",
},
};
},
methods: {
updateUsername(newUsername) {
this.user.username = newUsername;
},
},
};
</script>
<template>
<div>
<h1>欢迎回来, {{ user.username }}</h1>
<UserProfile />
</div>
</template>
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 />
</>;
}
表单输入
输入框
Vue 2
<script>
export default {
data() {
return {
text: "你好世界",
};
},
};
</script>
<template>
<div>
<p>{{ text }}</p>
<input v-model="text" />
</div>
</template>
复选框
Vue 2
<script>
export default {
data() {
return {
isAvailable: true,
};
},
};
</script>
<template>
<div>
<input id="is-available" v-model="isAvailable" type="checkbox" />
<label for="is-available">这是一个checkbox</label>
</div>
</template>
单选框
Vue 2
<script>
export default {
data() {
return {
picked: "red",
};
},
};
</script>
<template>
<div>
<div>你选择了: {{ picked }}</div>
<input id="blue-pill" v-model="picked" type="radio" value="blue" />
<label for="blue-pill">蓝色</label>
<input id="red-pill" v-model="picked" type="radio" value="red" />
<label for="red-pill">红色</label>
</div>
</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>
</>;
}
选择器
Vue 2
<script>
export default {
data() {
return {
selectedColorId: 2,
colors: [
{ id: 1, text: "红" },
{ id: 2, text: "蓝" },
{ id: 3, text: "绿" },
{ id: 4, text: "灰", isDisabled: true },
],
};
},
};
</script>
<template>
<select v-model="selectedColorId">
<option
v-for="color in colors"
:key="color.id"
:value="color.id"
:disabled="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应用特性
渲染应用
Vue 2
<!doctype html>
<html>
<body>
<div id="app"></div>
<script type="module" src="./main.js"></script>
</body>
</html>
Ripple
<!doctype html>
<html>
<body>
<div id="app"></div>
<script type="module" src="./main.js"></script>
</body>
</html>
fetch获取数据
Vue 2
<script>
export default {
data() {
return {
isLoading: false,
error: undefined,
users: undefined,
};
},
mounted() {
this.fetchData();
},
methods: {
async fetchData() {
this.isLoading = true;
try {
const response = await fetch("https://randomuser.me/api/?results=3");
const { results: users } = await response.json();
this.users = users;
this.error = undefined;
} catch (error) {
this.error = error;
} finally {
this.users = undefined;
this.isLoading = false;
}
},
},
};
</script>
<template>
<p v-if="isLoading">正在获取用户信息……</p>
<p v-else-if="error">获取用户信息出错</p>
<ul v-else-if="users">
<li v-for="user in users" :key="user.login.uuid">
<img :src="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>
}
</>;
}