vue的v-model指令原理分析

<!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">
  <div>使用v-on:input="change" :value="num": {{num}}</div>
  <input type="text"v-on:input="change" :value="num">

  <p>使用v-model给input绑定变量num2: {{num2}}</p>
  <input type="text" v-model="num2">

  <p>使用document.getElementById('input').oninput: 打印输入的数值: <span id="result"></span> </p>
  <input type="text" id="input">
</div>
<script src="https://unpkg.com/vue"></script>
<script>
  new Vue({
    el: '#app',
    data: {
      num: 1,
      num2: 1
    },
    methods: {
      change(e) {
        this.num = e.target.value
        console.log(this.num)
      }
    }
  })
  document.getElementById('input').oninput = function(e) {
    document.getElementById('result').innerText = e.target.value
  }
</script>
</body>
</html>

  运行结果:

  

参考文档: http://www.jb51.net/article/113112.htm

官方文档:http://cn.vuejs.org/v2/guide/components.html#使用自定义事件的表单输入组件

posted @ 2017-09-15 09:45  zph前端  阅读(429)  评论(0编辑  收藏  举报