vue、react前端框架实现TodoList页面案例

原始TodoList网页 (主要就是链接里网页应用ndex.html、styles.css、script.js ):

https://blog.csdn.net/weixin_42357472/article/details/140657576
node、npn安装参考:

https://blog.csdn.net/weixin_42357472/article/details/140643624

vue、react区别

Vue:Vue 有自己的脚手架工具 Vue CLI,可以快速创建项目。Vue 还有自己的服务器渲染框架 Nuxt.js。

React:React 有自己的脚手架工具 Create React App,可以快速创建项目。React 还有自己的服务器渲染框架 Next.js。

1、vue框架实现TodoList页面案例

安装vue:

bash 复制代码
npm install -g @vue/cli
或
npm install -g cnpm --registry=https://registry.npm.taobao.org


创建应用:

bash 复制代码
vue create my-vue-project


进入项目写应用:

bash 复制代码
cd my-vue-project

#修改以下文件:
src/App.vue

src/App.vue代码

bash 复制代码
<template>
  <div id="app">
    <h1>TodoList</h1>
    <form @submit.prevent="addTodo">
      <input v-model="newTodo" placeholder="Add a new todo">
      <button type="submit">Add</button>
    </form>
    <ul>
      <li v-for="(todo, index) in todos" :key="index" :class="{ completed: todo.completed }">
        <input type="checkbox" v-model="todo.completed" @change="saveTodos">
        <span>{{ todo.text }}</span>
        <button @click="deleteTodo(index)">Delete</button>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'App',
  data() {
    return {
      newTodo: '',
      todos: []
    }
  },
  methods: {
    loadTodos() {
      const savedTodos = localStorage.getItem('todos')
      if (savedTodos) {
        this.todos = JSON.parse(savedTodos)
      }
    },
    saveTodos() {
      localStorage.setItem('todos', JSON.stringify(this.todos))
    },
    addTodo() {
      if (this.newTodo.trim() === '') return
      this.todos.unshift({ text: this.newTodo, completed: false })
      this.newTodo = ''
      this.saveTodos()
    },
    deleteTodo(index) {
      this.todos.splice(index, 1)
      this.saveTodos()
    }
  },
  mounted() {
    this.loadTodos()
  }
}
</script>

<style>
#app {
  font-family: Arial, sans-serif;
  max-width: 500px;
  margin: 0 auto;
  padding: 20px;
}
h1 {
  text-align: center;
}
form {
  display: flex;
  margin-bottom: 20px;
}
input[type="text"] {
  flex-grow: 1;
  padding: 10px;
  font-size: 16px;
  border: 1px solid #ddd;
  border-radius: 4px 0 0 4px;
}
button {
  padding: 10px 20px;
  font-size: 16px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 0 4px 4px 0;
  cursor: pointer;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: flex;
  align-items: center;
  padding: 10px;
  background-color: #f9f9f9;
  border: 1px solid #ddd;
  margin-bottom: 10px;
  border-radius: 4px;
}
li.completed {
  text-decoration: line-through;
  opacity: 0.6;
}
li input[type="checkbox"] {
  margin-right: 10px;
}
li button {
  margin-left: auto;
  background-color: #f44336;
}
</style>

运行项目:

bash 复制代码
npm run serve

打开网页

1、react框架实现TodoList页面案例

安装好node自带了npx

创建应用:

bash 复制代码
npx create-react-app react-todolist
或
##安装create-react-app
cnpm install -g create-react-app
##创建应用
create-react-app react-todolist


进入项目写应用:

bash 复制代码
cd react-todolist

#修改以下文件:
src/App.js
src/App.css

src/App.js 代码

bash 复制代码
import React, { useState, useEffect } from 'react';
import './App.css';

function App() {
  const [todos, setTodos] = useState([]);
  const [newTodo, setNewTodo] = useState('');

  useEffect(() => {
    loadTodos();
  }, []);

  const loadTodos = () => {
    const savedTodos = localStorage.getItem('todos');
    if (savedTodos) {
      setTodos(JSON.parse(savedTodos));
    }
  };

  const saveTodos = (updatedTodos) => {
    localStorage.setItem('todos', JSON.stringify(updatedTodos));
  };

  const addTodo = (e) => {
    e.preventDefault();
    if (newTodo.trim() === '') return;
    const updatedTodos = [{ text: newTodo, completed: false }, ...todos];
    setTodos(updatedTodos);
    setNewTodo('');
    saveTodos(updatedTodos);
  };

  const toggleTodo = (index) => {
    const updatedTodos = todos.map((todo, i) => 
      i === index ? { ...todo, completed: !todo.completed } : todo
    );
    setTodos(updatedTodos);
    saveTodos(updatedTodos);
  };

  const deleteTodo = (index) => {
    const updatedTodos = todos.filter((_, i) => i !== index);
    setTodos(updatedTodos);
    saveTodos(updatedTodos);
  };

  return (
    <div className="App">
      <h1>TodoList</h1>
      <form onSubmit={addTodo}>
        <input 
          type="text" 
          value={newTodo} 
          onChange={(e) => setNewTodo(e.target.value)}
          placeholder="Add a new todo"
        />
        <button type="submit">Add</button>
      </form>
      <ul>
        {todos.map((todo, index) => (
          <li key={index} className={todo.completed ? 'completed' : ''}>
            <input 
              type="checkbox" 
              checked={todo.completed}
              onChange={() => toggleTodo(index)}
            />
            <span>{todo.text}</span>
            <button onClick={() => deleteTodo(index)}>Delete</button>
          </li>
        ))}
      </ul>
    </div>
  );
}

export default App;

src/App.css代码

bash 复制代码
.App {
  font-family: Arial, sans-serif;
  max-width: 500px;
  margin: 0 auto;
  padding: 20px;
}

h1 {
  text-align: center;
}

form {
  display: flex;
  margin-bottom: 20px;
}

input[type="text"] {
  flex-grow: 1;
  padding: 10px;
  font-size: 16px;
  border: 1px solid #ddd;
  border-radius: 4px 0 0 4px;
}

button {
  padding: 10px 20px;
  font-size: 16px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 0 4px 4px 0;
  cursor: pointer;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  display: flex;
  align-items: center;
  padding: 10px;
  background-color: #f9f9f9;
  border: 1px solid #ddd;
  margin-bottom: 10px;
  border-radius: 4px;
}

li.completed {
  text-decoration: line-through;
  opacity: 0.6;
}

li input[type="checkbox"] {
  margin-right: 10px;
}

li button {
  margin-left: auto;
  background-color: #f44336;
}


运行项目:

bash 复制代码
npm start

网页查看:

相关推荐
木古古189 分钟前
使用chrome 访问虚拟机Apache2 的默认页面,出现了ERR_ADDRESS_UNREACHABLE这个鸟问题
前端·chrome·apache
爱米的前端小笔记19 分钟前
前端八股自学笔记分享—页面布局(二)
前端·笔记·学习·面试·求职招聘
loey_ln41 分钟前
webpack配置和打包性能优化
前端·webpack·性能优化
建群新人小猿42 分钟前
会员等级经验问题
android·开发语言·前端·javascript·php
爱上语文43 分钟前
HTML和CSS 表单、表格练习
前端·css·html
djk88881 小时前
Layui Table 行号
前端·javascript·layui
NightCyberpunk2 小时前
HTML、CSS
前端·css·html
xcLeigh2 小时前
HTML5超酷响应式视频背景动画特效(六种风格,附源码)
前端·音视频·html5
zhenryx2 小时前
前端-react(class组件和Hooks)
前端·react.js·前端框架