简体   繁体   English

属性或方法“greet”未在实例上定义,但在渲染期间被引用

[英]Property or method “greet” is not defined on the instance but referenced during render

Same time I got an another error, which is "Invalid handler for event "click"".与此同时,我又遇到了另一个错误,即“事件“单击”的处理程序无效”。

<template>
    
    <div id="example-2">
      <!-- `greet` is the name of a method defined below -->
      <button v-on:click="greet">Greet</button>
    </div>
    
    </template>
    
    <script>
    window.onload = function () {
    
    var example2 = new Vue({
      el: '#example-2',
      data: {
        name: 'Vue.js'
      },
      // creating method greet
      methods: {
        greet: function (event) {
          // `this` inside methods points to the Vue instance
          alert('Hello ' + this.name + '!')
          // `event` is the native DOM event
          if (event) {
            alert(event.target.tagName)
          }
        }
      }
    })
    
    }
    
    </script>

the template tag is hidden when the HTML loads so you get the error cannot find element or something like that.加载 HTML 时, template标记是隐藏的,因此您会收到错误cannot find element或类似的信息。 You can load it with javascript.你可以用javascript加载它。

Vue.js does not use the built in template tag. Vue.js 不使用内置的template标签。

However if you using it over cdn try it like this但是,如果您通过 cdn 使用它,请尝试这样

  <div id="example-2">
    <button v-on:click="greet">Greet</button>
  </div>

  <script>
    var example2 = new Vue({
      el: "#example-2",
      data: {
        name: "Vue.js",
      },
      methods: {
        greet: function(event) {
          alert("Hello " + this.name + "!");
          if (event) {
            alert(event.target.tagName);
          }
        },
      },
    });
  </script>

What you can simply do is remove the template tags您可以简单地做的是删除template标签

Here is the working code: https://jsfiddle.net/t49zxdov/2/这是工作代码: https : //jsfiddle.net/t49zxdov/2/

<div id="example-2">
      <!-- `greet` is the name of a method defined below -->
      <button v-on:click="greet">Greet</button>
    </div>
    
window.onload = function () {
    
        var example2 = new Vue({
      el: '#example-2',
      data: {
        name: 'Vue.js'
      },
      // creating method greet
      methods: {
        greet: function (event) {
          // `this` inside methods points to the Vue instance
          alert('Hello ' + this.name + '!')
          // `event` is the native DOM event
          if (event) {
            alert(event.target.tagName)
          }
        }
      }
    })
    
    }

You should separate your vue template and the vue instance.您应该将 vue 模板和 vue 实例分开。 To do that,要做到这一点,

create file, ex: example.vue copy this code创建文件,例如: example.vue复制此代码

<template>
    <div id="example-2">
      <!-- `greet` is the name of a method defined below -->
      <button v-on:click="greet">Greet</button>
    </div>
</template>
<script>
  data() {
     return {
         name: 'Vue.js'
     }
  },
  methods: {
        greet: function (event) {
          // `this` inside methods points to the Vue instance
          alert('Hello ' + this.name + '!')
          // `event` is the native DOM event
          if (event) {
            alert(event.target.tagName)
          }
        }
      }
</script>

and create this file, ex: app.js并创建此文件,例如: app.js

const app = new Vue({
    el: '#example-2', 
});

and then link your app.js to your index layout.然后将您的app.js链接到您的索引布局。

I hope you get my point.我希望你明白我的意思。 thanks谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未在实例上定义属性或方法(…),但在渲染期间引用了该属性或方法 - Property or method (…) is not defined on the instance but referenced during render 属性或方法“_”未在实例上定义,但在渲染期间引用 - Property or method "_" is not defined on the instance but referenced during render Nuxt mixin - 属性或方法未在实例上定义,但在渲染期间被引用 - Nuxt mixin - Property or method is not defined on the instance but referenced during render 属性或方法“名称”未在实例上定义,但在渲染期间被引用 - Property or method “Name” is not defined on the instance but referenced during render (Vuejs)属性或方法未在实例上定义,但在渲染期间引用 - (Vuejs) Property or method is not defined on the instance but referenced during render Vue.js:属性或方法未在实例上定义,但在渲染期间引用 - Vue.js : Property or method is not defined on the instance but referenced during render 属性或方法“ sendResetMail”未在实例上定义,但在渲染期间被引用 - Property or method “sendResetMail” is not defined on the instance but referenced during render [Vue 警告]:属性或方法“games”未在实例上定义,但在渲染期间被引用 - [Vue warn]: Property or method "games" is not defined on the instance but referenced during render Vuejs属性或方法未在实例上定义,但在渲染期间被引用 - Vuejs Property or method is not defined on the instance but referenced during render [Vue警告]:属性或方法“ v”未在实例上定义,但在渲染期间被引用 - [Vue warn]: Property or method “v” is not defined on the instance but referenced during render
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM