<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="https://unpkg.com/vue@next" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div id="root"></div>
</body>
</html>
<script>
//封装对List的操作
const listEffect=()=>{
const {reactive}=Vue;
const list=reactive([1,2]);
const addItem=(inputValue)=>{
list.push(inputValue)
}
return {
list,
addItem,
}
}
const inputEffect=()=>{
const {ref}=Vue;
const inputValue=ref('123');
const resetValue=()=>{
inputValue.value=""
}
return {
inputValue,
resetValue,
}
}
const app=Vue.createApp({
setup(){
//流程调度中转
const {inputValue,resetValue} = inputEffect()
const {list, addItem} = listEffect() //获取函数执行的返回值
const addItemToList=()=>{
addItem(inputValue.value)//先添加
resetValue()//再清空
}
return {
list,
addItemToList,
inputValue,
}
},
template:`
<div>
<div>
<input v-model="inputValue" />
<button @click="addItemToList">添加</button>
</div>
<ul>
<li v-for="(item,index) in list" :key="index">{{item}}</li>
</ul>
</div>
`
})
const vm=app.mount("#root")
</script>