Javascript Vue.js:简单的点击功能不触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43242322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vue.js: Simple click function not firing
提问by Jeremy Thomas
I have a very simple app:
我有一个非常简单的应用程序:
<div id="show_vue">
<page-change page="bio" @click="changeThePage"></page-change>
<page-change page="health" @click="changeThePage"></page-change>
<page-change page="finance" @click="changeThePage"></page-change>
<page-change page="images" @click="changeThePage"></page-change>
</div>
Vue.component("page-change", {
template: "<button class='btn btn-success'>Button</button>",
props: ["page"]
})
var clients = new Vue({
el: '#show_vue',
data: {
currentRoute: window.location.href
},
methods: {
changeThePage: function() {
console.log("this is working")
}
}
})
...but when I click the <page-change></page-change>button, nothing is logged to the console. I know I'm missing something simple but I'm not getting any errors.
...但是当我单击<page-change></page-change>按钮时,控制台没有记录任何内容。我知道我错过了一些简单的东西,但我没有收到任何错误。
How do I make my click fire changeThePage
如何让我的点击火起来 changeThePage
回答by Happyriwan
When you do :
当你这样做时:
<page-change page="bio" @click="changeThePage"></page-change>
That means that your are waiting page-changecomponent emit the clickevent.
这意味着您正在等待page-change组件发出click事件。
Best solution (thanks to @aeharding) : Use .native event modifier
最佳解决方案(感谢@aeharding):使用 .native 事件修饰符
<page-change page="bio" @click.native="changeThePage"></page-change>
Solution 1 : emit click event from child component :
解决方案 1:从子组件发出 click 事件:
Vue.component("page-change", {
template: "<button @click='clicked' class='btn btn-success'>Button</button>",
props: ["page"],
methods: {
clicked: function(event) {
this.$emit('click', this.page, event);
}
}
})
For information eventis the default value passed by Vue for native event like click: DOM event
有关信息event是 Vue 为原生事件传递的默认值,例如click:DOM 事件
Solution 2 : emit directly from parent component :
解决方案 2:直接从父组件发出:
Vue.component("page-change", {
template: "<button class='btn btn-success'>Button {{ page }}</button>",
props: ["page"]
})
var clients = new Vue({
el: '#show_vue',
data: {
currentRoute: window.location.href,
pages: [
'bio', 'health',
'finance', 'images'
]
},
methods: {
changeThePage: function(page, index) {
console.log("this is working. Page:", page, '. Index:', index)
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.2.5/vue.js"></script>
<div id="show_vue">
<span v-for="(page, index) in pages" :key="index+page"
@click="changeThePage(page, index)">
<page-change :page="page"></page-change>
</span>
</div>
回答by aeharding
The best way to do this is to use the .nativeevent modifier.
最好的方法是使用.native事件修饰符。
For example:
例如:
<my-custom-component @click.native="login()">
Login
</my-custom-component>
Source: https://vuejs.org/v2/guide/components.html#Binding-Native-Events-to-Components
来源:https: //vuejs.org/v2/guide/components.html#Binding-Native-Events-to-Components
回答by Kenneth Mwangi
apparently v-on:clickseems to work better with the .nativeevent modifier.
显然v-on:click,使用.native事件修饰符似乎效果更好。
try page="bio" v-on:click.native="changeThePage()"></page-change>. It worked for me.
试试page="bio" v-on:click.native="changeThePage()"></page-change>。它对我有用。

