通过Vue小型项目todoList清单搞懂组件化编码流程,建议收藏
    		       		warning:
    		            这篇文章距离上次修改已过440天,其中的内容可能已经有所变动。
    		        
        		                
                
<template>
  <div id="app">
    <h2>ToDo List</h2>
    <input type="text" v-model="inputValue"/>
    <button @click="handleSubmit">提交</button>
    <ul>
      <li v-for="(item, index) in list" :key="index">
        {{ item }}
        <button @click="handleDelete(index)">删除</button>
      </li>
    </ul>
  </div>
</template>
 
<script>
export default {
  name: 'App',
  data() {
    return {
      inputValue: '',
      list: []
    }
  },
  methods: {
    handleSubmit() {
      if (this.inputValue.trim() === '') {
        alert('输入内容不能为空!');
        return;
      }
      this.list.push(this.inputValue.trim());
      this.inputValue = '';
    },
    handleDelete(index) {
      this.list.splice(index, 1);
    }
  }
}
</script>
 
<style>
#app {
  text-align: center;
}
 
h2 {
  margin-bottom: 20px;
}
 
input {
  margin-bottom: 10px;
  padding: 8px;
}
 
button {
  margin-right: 10px;
  padding: 8px 16px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
 
button:hover {
  background-color: #0056b3;
}
 
ul {
  list-style-type: none;
  padding: 0;
}
 
li {
  margin: 8px;
  padding: 8px;
  background-color: #f9f9f9;
  border: 1px solid #ddd;
}
</style>这段代码展示了如何使用Vue.js创建一个简单的ToDo List应用。用户可以输入任务,点击提交按钮将其添加到列表中。每个任务旁边有一个删除按钮,点击可以将其从列表中移除。代码中包含了对应的HTML模板、JavaScript脚本和CSS样式,体现了Vue组件化开发的基本流程。
评论已关闭