vue2 使用 oninput onChange报错Uncaught ReferenceError: oninputHandler is not defined

导读:本篇文章讲解 vue2 使用 oninput onChange报错Uncaught ReferenceError: oninputHandler is not defined,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

vue2使用oninput绑定事件报错:

         <input type=”text” id=’input’ :value=”msg” οninput=”oninputHandler()”>

(index):1 Uncaught ReferenceError: oninputHandler is not defined
    at HTMLInputElement.oninput ((index):1:1)

vue2 使用 oninput onChange报错Uncaught ReferenceError: oninputHandler is not defined

源码如下:

<template>
  <div>
    <h2>深入v-model</h2>
    <input type="text" v-model="msg">
    <span>{{ msg }}</span>
    <hr/>
    <h2>v-model实现原理</h2>
      <input type="text" id='input' :value="msg" oninput="oninputHandler()">
    <span>{{ msg }}</span>
  </div>
</template>

<script>

export default {
  name: "ModelTest",
  data() {
    return {
      msg: "HI - HI"
    }
  },
  methods: {
    oninputHandler(e) {
      let changeVal = document.getElementById("input");
      this.msg = changeVal.value;
      console.log(changeVal.value)
    }
  }
}
</script>

<style scoped>

</style>

分析 :

在html练习:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<input type="text" id="input" oninput="handleInput()"></input>
<span id="num"></span>
<span id="num"></span>
<script>
    function handleInput(e) {
// 处理事件代码

        var changeVal = document.getElementById("input");
        var showVal = document.getElementById("num");
        showVal.innerText = changeVal.value;
        console.log(changeVal.value)

    }
</script>


</body>
</html>

运行:监听oninput事件完全是OK的 

vue2 使用 oninput onChange报错Uncaught ReferenceError: oninputHandler is not defined

解决:

错误写法:

在vue2中这样写是没有作用的:οninput=‘function()’, 

oninput正确写法: @input=”oninputHandler()

<input type="text" id='input' :value="msg" @input="oninputHandler()">

 或者: v-on:input=”oninputHandler()”

      <input type="text" id='input' :value="msg" v-on:input="oninputHandler()">

执行效果:

vue2 使用 oninput onChange报错Uncaught ReferenceError: oninputHandler is not defined

扩展:onchange事件实现 

错误写法:

在html中onchange事件,在vue中也是不能直接写成οnchange=’func()’ ,
否则也会报类似的错,

VM7180 model:18 Uncaught ReferenceError: oninputHandler is not defined
    at HTMLInputElement.onchange (VM7180 model:18:2)

正确写法:
@change=’func()’

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/101531.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!