Vue(三)两个案例(todolist,tableBar)

待办事项:

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style>
            #addItem{
        font-size: 28px;
        vertical-align: middle;  
    }
    #txt{
        height: 30px;
        width: 250px;
        vertical-align: middle;
    }
    li{
        list-style: none;
        width: 400px;
        height: 70px;
        line-height: 70px;
        border: 1px solid lightgreen;
    }
    li:hover{
        background: lightcoral;
    }
</style>
        <script src="./vue.js"></script>
    </head>

    <body>
        <div id="box">
            <input id="txt" type="text"><button id="addItem" v-on:click='createList'>+</button>
            <ul>
                <li v-for='(item,index) in list'>{{item.value}}
                    <span v-if='item.sure' style="color:red;font-size: 18px">Be Sure</span>
                    <button v-on:click="delItem(index)">Del</button>
                    <button v-if='!item.sure' v-on:click='item.sure=true'>Sure</button>
                </li>
            </ul>
        </div>
        <script>
            let add = new Vue({
                el: '#box',
                data: {
                    list: []
                },
                methods: {
                    createList() {
                        this.list.push({
                            value: txt.value,
                            sure: false
                        });
                    },
                    delItem(num){
                        this.list.splice(num,1);
                    }
                }
            });
        </script>
    </body>

</html>

table选项卡:

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style>
            li {
                list-style: none;
                text-align: center;
            }

            #box {
                width: 280px;
                margin: 50px auto;
                background: lightblue;
            }

            #box:after {
                content: '';
                height: 0;
                clear: both;
                visibility: hidden;
                display: block;
            }

            #title li {
                width: 50px;
                height: 30px;
                float: left;
                padding: 20px 15px;
            }

            #title li:hover {
                background: lightcoral;
            }

            #content {
                display: block;
            }
        </style>
        <script src="./vue.js"></script>
    </head>

    <body>
        <div id="box">
            <ul id="title">
                <li v-for='(item,index) in list' v-on:click='changeContent(index)'>{{index}}</li>
            </ul>
            <ul id="content">
                <li v-for='(item,index) in list[str]'>{{item}}</li>
            </ul>
        </div>
        <script>
            var vm = new Vue({
                el: '#box',
                data: {
                    list: {
                        'movie': ['aaa', 'bbb', 'ccc', 'ddd', 'aaa', 'bbb', 'ccc', 'ddd', 'aaa', 'bbb', 'ccc',
                            'ddd', 'aaa', 'bbb', 'ccc', 'ddd'
                        ],
                        'game': ['eee', 'fff', 'ggg', 'hhh', 'eee', 'fff', 'ggg', 'hhh', 'eee', 'fff', 'ggg',
                            'hhh', 'eee', 'fff', 'ggg', 'hhh', 'eee', 'fff', 'ggg', 'hhh'
                        ],
                        'music': ['iii', 'jjj', 'kkk', 'lll', 'mmm', 'iii', 'jjj', 'kkk', 'lll', 'mmm', 'iii',
                            'jjj', 'kkk', 'lll', 'mmm', 'iii', 'jjj', 'kkk', 'lll', 'mmm', 'iii', 'jjj',
                            'kkk', 'lll', 'mmm'
                        ]
                    },
                    str: ''
                },
                methods: {
                    changeContent(data) {
                        this.str = data;
                    }
                }
            });
        </script>
    </body>

</html>

 

好的,我来给你写一个todolist。 首先,我们需要创建一个空的Vue 3项目,可以使用Vue CLI来创建。 在终端中输入以下命令: ``` vue create todolist ``` 接着,我们需要安装一些必要的依赖,包括`vuex`和`axios`。 在终端中输入以下命令: ``` cd todolist npm install vuex axios ``` 接下来,我们需要在src目录下创建一个名为`components`的文件夹,并在其中创建一个名为`TodoList.vue`的文件。 在`TodoList.vue`中,我们可以写下以下代码: ```html <template> <div> <h1>Todolist</h1> <input type="text" v-model="newTodo" @keydown.enter="addTodo" placeholder="Add new todo"> <ul> <li v-for="(todo, index) in todos" :key="todo.id"> <input type="checkbox" v-model="todo.completed" /> <span :class="{ completed: todo.completed }">{{ todo.title }}</span> <button @click="deleteTodo(index)">Delete</button> </li> </ul> </div> </template> <script> import { reactive } from 'vue'; import axios from 'axios'; export default { name: 'TodoList', setup() { const state = reactive({ todos: [], newTodo: '', }); const fetchData = async () => { const response = await axios.get('https://jsonplaceholder.typicode.com/todos'); state.todos = response.data; }; const addTodo = () => { if (state.newTodo.length > 0) { const newTodo = { id: state.todos.length + 1, title: state.newTodo, completed: false, }; state.todos.push(newTodo); state.newTodo = ''; } }; const deleteTodo = (index) => { state.todos.splice(index, 1); }; return { state, fetchData, addTodo, deleteTodo, }; }, mounted() { this.fetchData(); }, }; </script> <style> .completed { text-decoration: line-through; } </style> ``` 在这个代码中,我们使用Vue 3的Composition API来实现了`reactive`函数,它允许我们定义一个响应式的状态对象。我们还使用了`axios`来从外部API获取到了一些假数据,并将其渲染到了页面中。 现在我们只需要在`App.vue`中引入`TodoList.vue`组件即可: ```html <template> <div id="app"> <TodoList /> </div> </template> <script> import TodoList from './components/TodoList.vue'; export default { name: 'App', components: { TodoList, }, }; </script> ``` 现在我们已经成了一个简单的todolist应用程序。当你运行`npm run serve`时,你应该可以看到一个具有添加和删除功能的todolist
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿宇的编程之旅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值