当前位置:首页 > VUE

vscode实现vue的todolist

2026-01-20 21:39:56VUE

安装必要工具

确保已安装最新版VSCode,推荐安装以下扩展:

  • Volar(Vue官方推荐的语言支持插件)
  • Vue VSCode Snippets(代码片段快捷生成)
  • ESLint(代码规范检查)

通过终端全局安装Vue CLI:

npm install -g @vue/cli

创建Vue项目

在目标目录执行脚手架命令:

vue create vue-todolist

选择Vue 3预设配置,安装完成后进入项目目录:

cd vue-todolist

项目结构修改

删除默认的src/components/HelloWorld.vue,新建以下文件:

src/
├── components/
│   ├── TodoItem.vue
│   └── TodoForm.vue
├── App.vue
└── main.js

核心代码实现

App.vue 主组件

<template>
  <div id="app">
    <h1>Todo List</h1>
    <TodoForm @add-todo="addTodo" />
    <ul>
      <TodoItem 
        v-for="todo in todos" 
        :key="todo.id"
        :todo="todo"
        @toggle-complete="toggleComplete"
        @delete-todo="deleteTodo"
      />
    </ul>
  </div>
</template>

<script>
import TodoItem from './components/TodoItem.vue'
import TodoForm from './components/TodoForm.vue'

export default {
  components: { TodoItem, TodoForm },
  data() {
    return {
      todos: [
        { id: 1, text: 'Learn Vue', completed: false },
        { id: 2, text: 'Build project', completed: false }
      ]
    }
  },
  methods: {
    addTodo(newTodo) {
      this.todos.push({
        id: Date.now(),
        text: newTodo,
        completed: false
      })
    },
    toggleComplete(id) {
      const todo = this.todos.find(t => t.id === id)
      if (todo) todo.completed = !todo.completed
    },
    deleteTodo(id) {
      this.todos = this.todos.filter(todo => todo.id !== id)
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  max-width: 600px;
  margin: 0 auto;
}
ul {
  list-style: none;
  padding: 0;
}
</style>

TodoItem.vue 子组件

<template>
  <li :class="{ completed: todo.completed }">
    <input 
      type="checkbox" 
      :checked="todo.completed"
      @change="$emit('toggle-complete', todo.id)"
    />
    <span>{{ todo.text }}</span>
    <button @click="$emit('delete-todo', todo.id)">Delete</button>
  </li>
</template>

<script>
export default {
  props: {
    todo: {
      type: Object,
      required: true
    }
  }
}
</script>

<style scoped>
li {
  display: flex;
  align-items: center;
  margin: 10px 0;
}
.completed span {
  text-decoration: line-through;
  color: #888;
}
button {
  margin-left: auto;
  background: #ff4444;
  color: white;
  border: none;
  padding: 5px 10px;
  border-radius: 3px;
  cursor: pointer;
}
</style>

TodoForm.vue 子组件

<template>
  <form @submit.prevent="handleSubmit">
    <input 
      type="text" 
      v-model="newTodo" 
      placeholder="Add new todo..."
      required
    />
    <button type="submit">Add</button>
  </form>
</template>

<script>
export default {
  data() {
    return {
      newTodo: ''
    }
  },
  methods: {
    handleSubmit() {
      this.$emit('add-todo', this.newTodo)
      this.newTodo = ''
    }
  }
}
</script>

<style scoped>
form {
  display: flex;
  margin-bottom: 20px;
}
input {
  flex: 1;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px 0 0 4px;
}
button {
  padding: 8px 15px;
  background: #42b983;
  color: white;
  border: none;
  border-radius: 0 4px 4px 0;
  cursor: pointer;
}
</style>

运行项目

在VSCode终端启动开发服务器:

npm run serve

访问http://localhost:8080即可看到运行效果。使用Ctrl+C可停止服务。

项目优化建议

添加状态管理(可选安装Pinia):

npm install pinia

创建store/todoStore.js

import { defineStore } from 'pinia'

export const useTodoStore = defineStore('todos', {
  state: () => ({
    todos: []
  }),
  actions: {
    addTodo(text) {
      this.todos.push({
        id: Date.now(),
        text,
        completed: false
      })
    },
    // 其他操作方法...
  }
})

vscode实现vue的todolist

标签: vscodevue
分享给朋友:

相关文章

vue实现绑卡的原理

vue实现绑卡的原理

Vue 实现绑卡的原理 Vue 实现绑卡功能通常涉及前端表单处理、数据绑定、验证以及和后端 API 的交互。以下是实现绑卡功能的核心原理和步骤: 数据绑定与表单处理 Vue 通过 v-model 指…

vue实现https

vue实现https

Vue 中实现 HTTPS 的步骤 在 Vue 项目中实现 HTTPS 主要涉及开发环境配置和生产环境部署。以下是具体方法: 开发环境配置(Vue CLI 或 Vite) 使用 mkcert…

vue实现slider

vue实现slider

Vue 实现 Slider 组件 使用 Vue 实现 Slider 组件可以通过原生开发或借助第三方库。以下是两种常见方法: 原生实现 Slider 创建一个基础的 Slider 组件,通过 v-m…

vue实现滑块

vue实现滑块

Vue 实现滑块组件的方法 使用原生 HTML 和 Vue 指令 通过 Vue 的 v-model 和事件监听实现基础滑块功能。创建一个包含 input 元素的组件,类型设置为 range,并绑定到…

vue 全景实现

vue 全景实现

Vue 全景实现方案 在Vue中实现全景效果,通常需要结合WebGL或第三方库来处理3D场景渲染。以下是几种常见方法: 使用Three.js库 Three.js是一个强大的WebGL库,适合创建…

vue实现toast

vue实现toast

Vue 实现 Toast 的方法 使用第三方库(推荐) 对于快速实现 Toast 功能,推荐使用成熟的第三方库如 vue-toastification 或 vant 的 Toast 组件。 安装…