Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vuejs2 在组件上发出自定义事件_Vuejs2 - Fatal编程技术网

Vuejs2 在组件上发出自定义事件

Vuejs2 在组件上发出自定义事件,vuejs2,Vuejs2,我遵循上的“组件基础知识”指南,但无法按照指南生成结果。我不知道是将methods属性放在组件上还是放在根Vue实例上 当我将方法放在组件内部的increaseCount上时,会抛出一个错误 但是,当我将方法放在根Vue实例中的increaseCount中时,尽管没有错误,但当我单击按钮时,不会应用任何内容 // JS Vue.component('button-counter', { data: function(){ return {count: 0} } template

我遵循上的“组件基础知识”指南,但无法按照指南生成结果。我不知道是将
methods
属性放在组件上还是放在根Vue实例上

当我将方法
放在组件内部的increaseCount
上时,会抛出一个错误

但是,当我将方法
放在根Vue实例中的increaseCount
中时,尽管没有错误,但当我单击按钮时,不会应用任何内容

// JS
Vue.component('button-counter', {
    data: function(){ return {count: 0} }
    template: `<button v-on:click="$emit('increase-count')">You clicked me {{ count }} times.</button>`,
    methods: {
        onIncreaseCount: function(){ this.count++ }
    }
})
new Vue({
    el: "#main"
})
// HTML
<main class="main" id="main">
    <button-counter title="Example component" @increase-count="onIncreaseCount"></button-counter>
</main>
//JS
Vue.组件(“按钮计数器”{
数据:函数(){return{count:0}
模板:`你点击了我{{count}}}次。`,
方法:{
onIncreaseCount:function(){this.count++}
}
})
新Vue({
el:“主”
})
//HTML

我希望每次单击按钮时,
{{count}
值都会增加,但不会改变。

您不需要任何emit js,但必须调用increase函数

组件本身中的句柄

// JS
Vue.component('button-counter', {
    data: function(){ return {count: 0} }
    template: `<button v-on:click="increaseCount">You clicked me {{ count }} times.</button>`,
    methods: {
        increaseCount: function(){ this.count++ }
    }
})
new Vue({
    el: "#main"
})
// HTML
<main class="main" id="main">
    <button-counter title="Example component"></button-counter>
</main>
//JS
Vue.组件(“按钮计数器”{
数据:函数(){return{count:0}
模板:`你点击了我{{count}}}次。`,
方法:{
increaseCount:function(){this.count++}
}
})
新Vue({
el:“主”
})
//HTML

您不需要任何emit js,但必须调用increase函数

组件本身中的句柄

// JS
Vue.component('button-counter', {
    data: function(){ return {count: 0} }
    template: `<button v-on:click="increaseCount">You clicked me {{ count }} times.</button>`,
    methods: {
        increaseCount: function(){ this.count++ }
    }
})
new Vue({
    el: "#main"
})
// HTML
<main class="main" id="main">
    <button-counter title="Example component"></button-counter>
</main>
//JS
Vue.组件(“按钮计数器”{
数据:函数(){return{count:0}
模板:`你点击了我{{count}}}次。`,
方法:{
increaseCount:function(){this.count++}
}
})
新Vue({
el:“主”
})
//HTML

是的,我似乎误解了发射的概念。它用于组件和父实例(父组件或本指南中的根实例)之间的通信。是的,似乎我误解了发射的概念。它用于组件和父实例(父组件或本指南中的根实例)之间的通信