vue watch方法 观察变量的变化 v-show,v-bind,v-on,v-model

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

<body>
    <div id="app">
        {{a}}
    </div>
    <!-- 开发环境版本,包含了有帮助的命令行警告 -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
        var data = { a: 1 }
        var app = new Vue({
            el: '#app',
            data: data
        })
        // $watch 是一个实例方法
        app.$watch('a', function (newValue, oldValue) {
            // 这个回调将在 `vm.a` 改变后调用
            console.log(newValue,oldValue)
        })
        data.a=2
    </script>
</body>

</html>

在控制台可以看到a的两次数值

v-show

在标签中使用
<div show="true">1</div>
通过""的值来决定标签的内容是否显示,可以使用表达式

v-bind

用来设置元素的属性

v-on

用来绑定一些事件,比如点击

<div id="app-5">
  <p>{{ message }}</p>
  <button v-on:click="reverseMessage">反转消息</button>
</div>
var app5 = new Vue({
  el: '#app-5',
  data: {
    message: 'Hello Vue.js!'
  },
  methods: {
    reverseMessage: function () {
      this.message = this.message.split('').reverse().join('')
    }
  }
})

传递参数

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

<body>
    <div id="app">
        <input type="button" value="点击" @click="doIt(666)">
    </div>
    <!-- 开发环境版本,包含了有帮助的命令行警告 -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                message: 'Hello Vue!'
            },
            methods:{
                doIt:function(x){
                    console.log("我也不知道要干什么")
                    console.log(x)
                }
            }
        })
    </script>
</body>

</html>

v-model

获取和设置表单的数据(双向绑定)

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

<body>
    <div id="app">
        <input type="text" v-model="message">
        {{message}}
    </div>
    <!-- 开发环境版本,包含了有帮助的命令行警告 -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                message: 'Hello Vue!'
            }
        })
    </script>
</body>

</html>
posted @ 2021-06-21 21:43  一个经常掉线的人  阅读(519)  评论(0)    收藏  举报