如果想实现每次提交完input框内容清空,要如何实现?

来源:2-8 使用 Composition API 开发TodoList(2)

PADAopanda

2021-09-23 17:27:47

<script>
// 关于 list 操作内容进行封装
const listRelativeEffect = () => {
const { reactive } = Vue;
const list = reactive([]);
const addItemToList = (item) => {
list.push(item);
}
return { list, addItemToList }
}

// 关于 input 操作内容进行封装
const inputRelativeEffect = () => {
const { ref } = Vue;
const inputValue = ref('');
const handleInputValueChange = (e) => {
inputValue.value = e.target.value;
}
return { inputValue, handleInputValueChange }
}
const app = Vue.createApp({
setup() {
const { list, addItemToList } = listRelativeEffect();
const { inputValue, handleInputValueChange } = inputRelativeEffect();
return {
list, addItemToList,
inputValue, handleInputValueChange
}
},

// const app = Vue.createApp({
// setup(props, context) {
// const { ref, reactive } = Vue;
// const inputValue = ref('');
// const list = reactive([]);
// const handleInputValueChange = (e) => {
// // console.log(e.target.value);
// inputValue.value = e.target.value;
// }
// const handleSubmit = () => {
// list.push(inputValue.value);
// inputValue.value='';
// }
// return {
// inputValue,
// list,
// handleInputValueChange,
// handleSubmit,
// }
// },

template: `
<div>
<div>
<input :value="inputValue" @input="handleInputValueChange" />
<button @click="()=>addItemToList(inputValue)"> submit </button>
</div>
<ul>
<li v-for="(item,index) in list" :key=index>{{item}}</li>
</ul>
</div>
`,
});

const vm = app.mount('#root');
</script>

如果不封装,内容都放一起的话,调用也比较方便。但如果封装的话,要怎么清空input框呢?

const handleSubmit = () => {
​ list.push(inputValue.value);
inputValue.value='';
}
写回答

1回答

好帮手慕星星

2021-09-23

同学你好,可以在标签的点击事件中设置清空内容,如下:

https://img.mukewang.com/climg/614c57080977266f08760073.jpg

祝学习愉快!

0

0 学习 · 15276 问题

查看课程