vue使用的注意细节

85 阅读1分钟
<!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="./vue2.6.14.js"></script>
</head>

<body>
    <div id="app">
        {{msg}}
    </div>
    <script>
        new Vue({
            el: "#app",
            // 在全局时候使用
            data: {
                msg: "nihao"
            },
            // 在使用vue组件和全局的时候都可以使用
            // 在vue的属性方法中不可以使用箭头函数
            data: function() {
                return {
                    msg: "你好"
                }
            },
            methods: {
                /* vue的属性方法中不可以使用箭头函数,
                   因为箭头函数返回的this指向window,
                   不是指向Vue实例化的对象 */
                // add: () => {
                //     console.log(this);
                // }

                add() {
                    /* 在普通函数里面可以使用箭头函数 */
                    setTimeout(() => {
                        console.log(this);
                    }, 1000)
                }
            }
        })
    </script>
</body>

</html>