vue.js - VueJS 监听组件内部的外部事件
问题描述
我有一个自定义对讲启动器按钮组件,如下所示:
export default {
data : function() {
return ({
open : false,
notif_count : 0,
})
},
mounted : function() {
if (typeof Intercom !== 'undefined') {
Intercom('onUnreadCountChange', function(count) {
if (count) this.notif_count = count;
});
Intercom('onHide', function() {
this.open = false;
});
}
},
computed : {
...mapState({
HIGHER : (state) => state.intercom.higher,
})
},
}
显然该onHide
方法在这里不正确,如果在组件安装后触发,我listen
该如何更改它this.open
的值?onHide
感谢您的帮助!
解决方案
带有箭头功能的变体
export default
{
data()
{
return {
open : false,
notif_count : 0,
};
},
mounted()
{
if (typeof Intercom !== 'undefined')
{
Intercom('onUnreadCountChange', (count) =>
{
if (count) this.notif_count = count;
});
Intercom('onHide', () =>
{
this.open = false;
});
}
},
computed:
{
...mapState({
HIGHER : (state) => state.intercom.higher,
}),
},
}
带有组件方法的变体
export default
{
data()
{
return {
open : false,
notif_count : 0,
};
},
mounted()
{
if (typeof Intercom !== 'undefined')
{
Intercom('onUnreadCountChange', this.onUnreadCountChange);
Intercom('onHide', this.onHide);
}
},
computed:
{
...mapState({
HIGHER : (state) => state.intercom.higher,
}),
},
methods:
{
onUnreadCountChange(count)
{
if (count) this.notif_count = count;
},
onHide()
{
this.open = false;
},
}
}
推荐阅读
- react-native - 如何配置 react-native-router-flux android 返回历史记录?
- c# - 如何从模型中的 2 个连接表中获得过滤结果
- r - 重新排列 R 中的相关矩阵以获得 1 的诊断
- performance-testing - 了解 RPS on Locust 摘要结果
- hibernate - 有没有办法在由于 BatchUpdateException 而失败之前跟踪或获取 JPA 完成的批迭代总数?
- python - 找到模式后如何搜索模式?
- android - 如何使用 proguard 将文件保存在 assets 文件夹中?
- matlab - 如何将字符附加到现有的 csv 文件?
- mysql - Drupal 7 从 mysql 数据库恢复不起作用
- javascript - 使用javascript在aspx中回发后保持Div可见或隐藏