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

网页查看:

相关推荐
m0_548514774 分钟前
前端Pako.js 压缩解压库 与 Java 的 zlib 压缩与解压 的互通实现
java·前端·javascript
AndrewPerfect4 分钟前
xss csrf怎么预防?
前端·xss·csrf
Calm5507 分钟前
Vue3:uv-upload图片上传
前端·vue.js
浮游本尊11 分钟前
Nginx配置:如何在一个域名下运行两个网站
前端·javascript
m0_7482398312 分钟前
前端bug调试
前端·bug
m0_7482329214 分钟前
[项目][boost搜索引擎#4] cpp-httplib使用 log.hpp 前端 测试及总结
前端·搜索引擎
新中地GIS开发老师20 分钟前
《Vue进阶教程》(12)ref的实现详细教程
前端·javascript·vue.js·arcgis·前端框架·地理信息科学·地信
m0_7482495422 分钟前
前端:base64的作用
前端
html组态28 分钟前
web组态可视化编辑器
前端·物联网·编辑器·web组态·组态·组态软件
~央千澈~35 分钟前
如果你的网站是h5网站,如何将h5网站变成小程序-除开完整重做方法如何快速h5转小程序-h5网站转小程序的办法-优雅草央千澈
前端·apache