[英]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:这是一种解决方案:
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>
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.