Vue实例的方法 methods

<!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>
</head>

<body>

    <div id="app">
        <!-- 在模板中调用 methods 选项中方法 -->
        <button type="button" v-on:click="sayHello">{{greeting}}</button>
        <!-- <button type="button" @click="sayHello">{{greeting}}</button> -->
    </div>

    <script src="./vue.js"></script>

    <script>
        const app = new Vue({
            el: '#app',
            data: {
                greeting: 'Hello Vuejs'
            },
            methods: {
                sayHello() {
                    console.log('Hello World')
                    // 在方法中调用另一个方法,需要使用 this,this指的使用当前 Vue 实例,也就是 app
                    this.sayHi()
                },
                sayHi() {
                    // 在方法中访问 data 选项中的数据时,也需要使用 this
                    console.log(this.greeting)
                }
            }
        });
    </script>
</body>

</html>
发布了151 篇原创文章 · 获赞 1 · 访问量 1878

猜你喜欢

转载自blog.csdn.net/qq_45802159/article/details/103816447