admin管理员组文章数量:1336405
Everything in my app was working perfectly fine until I began to add my javascript. Now I continuously get errors in the console.
I get this error:
Property or method "show" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based ponents, by initializing the property.
As well as this error:
TypeError: _vm.show is not a function at click App.vue?d98c:25 at HTMLButtonElement.invoker vue.esm.js?efeb:1906
Desired Oute: Click on "loginBtn" alert prompts "click".
My code:
// app.vue script
export default {
name: 'app'
}
var show = new Vue({
el: '#loginBtn',
data: {
n: 0
},
methods: {
show: function(event) {
targetId = event.currentTarget.id;
alert('click')
}
}
})
<!-- the button -->
<template>
<div>
<button v-on:click="show($event)" id="loginBtn">Login</button>
</div>
</template>
Everything in my app was working perfectly fine until I began to add my javascript. Now I continuously get errors in the console.
I get this error:
Property or method "show" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based ponents, by initializing the property.
As well as this error:
TypeError: _vm.show is not a function at click App.vue?d98c:25 at HTMLButtonElement.invoker vue.esm.js?efeb:1906
Desired Oute: Click on "loginBtn" alert prompts "click".
My code:
// app.vue script
export default {
name: 'app'
}
var show = new Vue({
el: '#loginBtn',
data: {
n: 0
},
methods: {
show: function(event) {
targetId = event.currentTarget.id;
alert('click')
}
}
})
<!-- the button -->
<template>
<div>
<button v-on:click="show($event)" id="loginBtn">Login</button>
</div>
</template>
Share
Improve this question
edited Oct 16, 2017 at 17:01
thanksd
55.7k23 gold badges165 silver badges154 bronze badges
asked Oct 16, 2017 at 16:33
hannacreedhannacreed
6693 gold badges17 silver badges34 bronze badges
1 Answer
Reset to default 4You are using a Single-File Component (a .vue
file), which is a file format for a Vue ponent definition used by vue-loader
.
The script section of a .vue
file (what's inside the <script>
tag) should export an object specifying the definition of the Vue instance.
From the documentation:
The script must export a Vue.js ponent options object. Exporting an extended constructor created by Vue.extend() is also supported, but a plain object is preferred.
You are currently only exporting { name: 'app' }
, which is why Vue can't find the show
method.
Your <script>
section should look like this:
<script>
export default {
name: 'app',
data() {
return { n: 0 }
},
methods: {
show: function(event) {
targetId = event.currentTarget.id;
alert('click')
}
}
}
</script>
Note also that the data
property of the object exported needs to be a function returning the data properties. See the "Why does data
need to be a function" section of Vue's Common Beginner Gotchas page.
本文标签: javascriptProperty or method not defined in vue fileStack Overflow
版权声明:本文标题:javascript - Property or method not defined in .vue file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742325746a2453687.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论