Closed Dreampie closed 9 years ago
仔细看文档啊:http://vuejs.org/api/instance-methods.html#vm-\$dispatch\(_event\,_\[args…\]_)
冒泡的事件要用 $dispatch, $emit 只触发本身的事件。
我把emit改成dispatch也不行
父组件的 ready 触发比子组件晚。用 events 选项
非常感谢,已经触发了,怎么把 触发传递的 data 数据 给父级的 component?
<app-sidebar menus="{{menus}}"></app-sidebar>
<app-header tabs="{{tabs}}"></app-header>
谢谢 搞定了 ,关于 require 'vue-resource' 返回空,能看看吗,npm安装的
下面的代码 vm.$emit('input', this.value)
为什么要触发 input 事件呢? 这个 input 事件是谁的呢?@yyx990803 @Dreampie @yyx990803
Vue.component('select2', {
props: ['options', 'value'],
template: '#select2-template',
mounted: function () {
var vm = this
$(this.$el)
.val(this.value)
// init select2
.select2({ data: this.options })
// emit event on change.
.on('change', function () {
//alert(this.value);
vm.$emit('input', this.value)
})
},
....
多谢你的链接 @fnlctrl 刚才我自己做了一个例子,明白了。
hi, @fnlctrl 有个困惑的问题想请教您。
我在看 @Vladimir Kharlampidi 大神封装的 framework7-vue
源码的时候,看到 tab.vue
组件中自定义事件的时候不是很明白,代码如下:
tab.vue:
<template>
<div class="tab" :class="active ? 'active' : false" @tab:show="onTabShow" @tab:hide="onTabHide">
<slot></slot>
</div>
</template>
<script>
export default {
props: {
'active': Boolean
},
methods: {
onTabShow: function (e) {
this.$emit('tab:show', e);
},
onTabHide: function (e) {
this.$emit('tab:hide', e);
}
}
}
</script>
不明白的是,@tab:show
可以在当前实例上使用吗? 不应该是父组件在使用子组件的地方用 v-on 来监听子组件触发的事件吗?
然而,我仿造上面的代码在我的 button.vue
组件中点击没有反应。下面是我的代码:
button.vue
<template>
<button class="btn btn-success"
@button:open="onOpen"
@button:closed="onClosed">
<slot></slot>
</button>
</template>
<script>
export default {
methods: {
onOpen (event) {
console.log('button open ...')
this.$emit('button:open', event)
},
onClosed (event) {
console.log('button closed ...')
this.$emit('button:closed', event)
}
}
}
</script>
入口文件app.js
import Vue from 'vue'
import VueWebpack from '../src/vue-webpack.js'
Vue.use(VueWebpack)
const app = new Vue({
el: '#app',
methods: {
onOpenTest (event) {
alert('hello')
}
}
})
index.html
<body>
<div id="app">
<v-button @button:open="onOpenTest">button</v-button>
</div>
<script src="build.js"></script>
</body>
ps: 主要是 @tab:show="onTabShow" @tab:hide="onTabHide"
这点不是很明白。 @fnlctrl
//app
//view
not work?