简体   繁体   English

Nuxtjs 属性或方法未在实例上定义

[英]Nuxtjs property or method not defined on instance

I'm trying to get a counter to add 1 to a variable counter in a nuxtjs template.我正在尝试让一个计数器将 1 添加到 nuxtjs 模板中的变量计数器。 Not sure what is going on here:不确定这里发生了什么:

<template>
  <div class="container">
    <div>
      <h1>London Weather</h1>
      <div id="example-1">
        <button @click="test()">Add 1</button>
        <p>The button above has been clicked {{ counter }} times.</p>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  data: {
    counter: 0
  },
  methods: {
    test: function(counter) {
      this.counter += 1
      console.log(counter)
    }
  },
}
</script>

Here's one solution:这是一种解决方案:

  • Reference counter as this.counter引用计数器作为this.counter
  • data is a function which should return whatever data you need data是一个函数,它应该返回你需要的任何数据
  • test doesn't need to take parameters as you are just changing the component's state rather than modifying some parameter you're passing in test不需要接受参数,因为您只是在更改组件的状态而不是修改您传入的某些参数
<template>
  <div class="container">
    <div>
      <h1>London Weather</h1>
      <div id="example-1">
        <button @click="test">Add 1</button>
        <p>The button above has been clicked {{ counter }} times.</p>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  data() {
    return {
      counter: 0
    }
  },
  methods: {
    test: function() {
      this.counter += 1
      console.log(this.counter)
    }
  }
}
</script>

See it live here在这里观看直播

Try the following:请尝试以下操作:

 <template> ... <button @click="test(1)">Add 1</button> ... </template> <script> ... methods: { test(count) { this.counter += count; console.log(this.counter) } } ...

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

相关问题 带有Nuxtjs属性或方法的VueJS未在实例上定义,但在渲染期间被引用 - VueJS with Nuxtjs Property or method is not defined on the instance but referenced during render 未在实例上定义的属性或方法 - Property or method not defined on instance Vue:实例上未定义属性或方法 - Vue: Property or method is not defined on the instance VueJS:&#39;没有在实例上定义属性或方法&#39; - 即使方法是在实例上定义的 - VueJS: 'Property or method is not defined on the instance' - even though method is defined on instance [Vue 警告]:实例上未定义属性或方法“trendingMovies” - [Vue warn]: Property or method "trendingMovies" is not defined on the instance 在实例上未定义属性或方法“ orgs”,但已引用 - Property or method “orgs” is not defined on the instance but referenced 属性或方法未在实例上定义,但在渲染期间被引用 - Property or method is not defined on the instance but refenced during render 实例上未定义 Vue js 属性或方法“theSport” - Vue js Property or Method "theSport" not defined on the instance 属性或方法……未在实例上定义,但在渲染期间引用 - 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 it is
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM