Don*_*the 4 javascript vue.js vuejs2
我正在从子组件向其父组件发送事件.我想通过父节点中的方法拦截信号.但是无论是否发出事件,监听功能始终触发.请注意,我使用的是单个文件组件和Vue-router.
另外,我发现很少有VueJS示例使用单个文件组件,对于noob,从一个文件中的简单Vue应用程序转换为多个单个文件组件可能会令人困惑.
家长:
<template>
....html here
</template>
<script>
import Child from './Child.vue'
export default {
name: 'Parent',
data () {
return {
stage: 1
}
},
components: {
Child
},
created: function () {
// the following line always runs even when listen for non-existent event eg this.$on('nonsense'...)
this.$on('child-event', this.stage = 2)
}
}
Run Code Online (Sandbox Code Playgroud)
儿童:
<template>
<button v-on:click="sendEvent" type="button" class="btn btn-success">Next</button>
</template>
<script>
export default {
name: 'Child',
data () {
return {
response_status: 'accepted'
}
},
methods: {
sendEvent: function () {
this.$emit('child-event', 'accepted')
}
}
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?
设置on事件的另一种方法是直接从调用子组件的位置引用侦听器方法.
在您的父模板上,您可以执行以下操作:
<Child v-on:child-event="eventIsEmitted"></Child>
Run Code Online (Sandbox Code Playgroud)
在你的methods:
eventIsEmitted: function(status) {
if (status == 'accepted') {
this.stage = 2;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2916 次 |
| 最近记录: |