感谢大家帮帮忙
html部分
<div class="vue">
<!-- 组件绑定了一个事件 -->
<test-component v-on:on-ok="ok"></test-component>
</div>
js部分
//组件定义
var tc = {
template: '<div><button v-on:click="ok">click ok</button></div>',
methods: {
ok:function(){
this.$emit('on-ok');
//我想在这儿得到 'haha' 这个字符串
}
}
};
//根实例
new Vue({
el:'.vue',
components:{
'test-component': tc
},
methods: {
ok: function(){
return 'haha';
}
}
});
不太好看的解决方案
//组件中
this.$emit('on-ok', function(str){
alert(str);
});
//根实例中
ok: function(callback){
callback('haha');
}
在子组件想获取父组件的方法,可以使用this.$parent.ok()
//组件定义