3.Vue列表展示

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
</head>

<body>
    <!-- 基本列表的演示 -->
    <div id="app">
        <ul>
            <li v-for='item in movies'>{{item}}</li>
        </ul>
    </div>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                message: 'hello!',
                movies: ['星际穿越', '大话西游', '少年派', '盗梦空间']
            }
        })
        app.movies.push('海王'); // app是一个对象,message和movies都是app的一个属性
    </script>
    <hr>

    <!-- 计数器案例 -->
    <div id="app2">
        <p>当前计数:{{counter}}</p>
        <button v-on:click='counter++'>+</button> <!-- v-on 监听事件-->
        <button v-on:click='decrease'>-</button> <!-- 执行函数的话不能加括号 -->
        <button @click='add2'>+2</button> <!-- @click是v-on的简写,又称为语法糖 -->
    </div>
    <script>
        const app2 = new Vue({
            el: '#app2',
            data: {
                counter: 0
            },
            methods: {
                decrease: function () {
                    this.counter--;  // 要使用this调用counter
                },
                add2:function(){
                    this.counter += 2; 
                }
            }
        })
    </script>
</body>

</html>
上一篇:686 vue3 Composition API:setup的this、参数、返回值,rReactive ,ref,readonly,,,,,,,,,,,,,,,


下一篇:10-map/WeakMap/WeakSet的使用场景