2024-Vue/C1-基础语法/09.列表渲染.html

52 lines
1.6 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!-- 1. 导入vue.js的脚本文件 -->
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
<div id="app">
<!-- 添加数据 -->
<input type="text" v-model="name">
<button @click="addUser">添加</button>
<ul>
<li v-for="user in userList">姓名:{{user.name}}</li>
<li v-for="(user, index) in userList" :key="user.id">
<input type="checkbox" /> 姓名:{{user.name}}
</li>
</ul>
</div>
</body>
<script>
// 将 Vue 对象中的createApp reactive 属性赋值给createApp reactive
const { createApp, reactive, ref } = Vue; // 解构赋值语法
// 创建一个 Vue 的应用程序
createApp({
// 组合式API 的 setup 用于设置响应式数据和方法
setup() {
// 返回数据
const userList = reactive([
{ id: 1, name: "Tom" },
{ id: 2, name: "Jerry" },
{ id: 3, name: "Maxxie" }
])
const name = ref('');
const nextId = ref(4);
const addUser = () => {
userList.unshift({ id: nextId.value, name: name.value })
name.value = "";
nextId.value++;
}
return {
userList, addUser, name, nextId
}
}
}).mount('#app') // 将 Vue 应用程序挂载到app元素上
</script>
</html>