自学内容网 自学内容网

【vue】记事本案例

在这里插入图片描述

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <input type="text" v-model="data.content" placeholder="输入要添加进list的内容">

        <button @click="add">添加</button>
        <p>list中的内容如下:</p>
        <ul>
            <li v-for="(item, index) in data.list" :key="index">
                <!-- 从 list 数组中移除从 index 开始的一个元素。 -->
                {{ item }}<button @click="remove(index)">删除</button>
            </li>
        </ul>
        <p>当前list的长度为:{{ data.list.length }}</p>

        <button @click="clear">清空</button>

    </div>

    <script type="module">
        import { createApp, reactive, ref } from './vue.esm-browser.js'
        createApp({
            setup() {
                const data = reactive({
                    content: "",
                    list: ["1", "2","3"],
                })


                const add = () => {
                    data.list.push(data.content)
                    console.log(data.list)
                }

                const remove = (index) => {
                    data.list.splice(index, 1)
                }

                const clear = () => {
                    data.list = []
                }
                return {
                    data,
                    add,
                    remove,
                    clear
                }
            }
        }).mount("#app")
    </script>
</body>

</html>

参考

https://www.bilibili.com/video/BV1nV411Q7RX


原文地址:https://blog.csdn.net/m0_63070489/article/details/137696307

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!