- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我在 Vue 中使用全局事件总线已经有一段时间了——类似于 const bus = new Vue()
.工作正常,但是,订阅的处理可能会变得非常冗长。
假设我在一个组件中订阅了一个事件:
mounted() {
bus.$on('some.event', callback)
}
我必须跟踪回调并在 beforeDestroy
中正确处理它.这可以使用全局混合来稍微简化,但因为我使用的是 <keep-alive>
, 我必须区分 mounted
中的订阅和 activated
回调。
所以我想我应该给 Vuex 一个机会来管理它,因为 watchers 是由框架处理的。我提出了以下建议。
只要发布了对象或数组,似乎就可以正常工作。原始数据似乎不会触发 react ,尽管被包裹在外部对象中,即 { data: 123 }
我正在寻找有关通知订阅者的替代解决方案。到目前为止,我所看到的只是内部 notify
方法,使用起来感觉不太安全。
eventstore.js
import Vue from 'vue'
const state = {
events: {}
}
const actions = {
publish({commit}, payload) {
commit('publish_event', payload)
}
}
const mutations = {
publish_event(state, payload) {
if(!state.events[payload.key]) {
Vue.set(state.events, payload.key, { data: payload.data })
} else {
state.events[payload.key] = { data: payload.data }
}
}
}
const getters = {
events: state => state.events
}
export default {
state,
actions,
mutations,
getters
}
globalmixin.js
methods: {
publish(key, data) {
this.$store.dispatch('publish', { key, data })
}
}
somecomponent.vue
function mapEventGetters(eventKeys) {
return _.reduce(eventKeys, (result, current) => {
result[current] = function() {
return _.get(this, `$store.getters.events.${current}.data`)
}
return result
}, {})
}
computed: {
...mapEventGetters(['foo_bar'])
},
watch: {
'foo_bar'(value) {
console.log(`foo_bar changed to ${value}`)
}
}
最佳答案
这个API会打破Vuex的核心概念——Vuex的数据流。客户端将能够在 Vuex 中的任何地方改变/读取存储状态。
老实说,这种方式不需要在 Vuex 中实现,因为它只是一个事件发射器。我建议您在操作中使用一些事件发射器(可能是空的 Vue 实例)。
export const emitter = new Vue()
export default {
// ...
actions: {
// should be called when the store is initialized
// to observe events
observe({ dispatch, commit }) {
emitter.$on('some-event', () => {
commit('someEvent')
})
emitter.$on('other-event', () => {
dispatch('otherEvent')
})
},
// notify some event in action
notify({ state }) {
emitter.$emit('notify', state.someValue)
}
}
}
在github上搜索一次就解决了我的问题。垫是帮助你。谢谢!!
关于javascript - 使用 Vuex 的全局事件总线 - 始终通知订阅者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53597963/
目录 总线是什么? 常见总线类型有哪些? 总线的串行和并行的区别? 数据总线 地址总线
从下面的代码我在 map 上添加标记,每 15 秒刷新一次并从数据库中获取新的纬度和经度。标记(巴士图像)已成功添加到 map 上并从一个位置平稳移动到另一个位置,就像汽车在路上行驶一样。现在我想要的
如果在小端处理器上运行的程序将未缓存的值 0xaabbccdd 写入地址 0,并且处理器使用 32 位宽的 AXI4 总线,那么 WDATA 的第 31-24 位是 0xaa 还是 0xdd? AXI
是否存在用于将进程内服务消息传递给另一个服务的扭曲机制?我写了一个原型(prototype)总线,看起来像 from collections import defaultdict ch
我修改了设备树文件并使用 4 个 GPIO 引脚启用了 spi,这些引脚支持 pinmux 并从 gpio 切换到 spi 功能。但是在 Linux 内核代码中,代码如何知道使用了哪个 spi 总线/
我正在使用控创嵌入式计算机通过 I2C 与 ST 微 Controller 通信。我正在使用开发适配器与 I2C 接口(interface),使用描述的简单 read() 和 write() 函数 h
我有一个需要 PEC 的 I2C/SMBus 设备我正在为它编写一个内核空间驱动程序。 在 Linux 2.6.37 上我使用 i2c_board_info实例化客户端并在那里设置标志,但现在驱动程序
我想确认我的消息已经通过 socketCAN 库保存在 CAN 总线上。socketCAN 文档描述了使用 recvmsg() 函数时的这种可能性,我对其实现有疑问。 我要实现的功能是确认我的消息在仲
下面是我的代码 #import #import int main(int argc, const char *argv[]) { char *str = "First string";
在大量使用 D-Bus 的应用程序中获得更好的时间性能有哪些好的做法? ? 以下是我们的团队通过硬敲学校学到的一些知识: 尝试将数据实体组合成一个单一的大型结构/对象,以通过 D-Bus IPC 发送
我正在Akka内核下运行Akka应用程序,该程序在其他系统上也可以正常工作。 akka { loggers = ["akka.event.slf4j.Slf4jLogger"] log
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我是 C 语言新手,对于家庭作业考试,我必须实现一个简单的服务器套接字程序,该程序在循环中发送一些数据,并且如果客户端连接到服务器套接字(已使用 Arduino 完成,但需要相同的功能)在 raspb
我正在构建一个由许多(> 100)个相同节点组成的系统,所有节点均通过 CAN 总线连接。这个想法是所有节点必须具有相同的信息,任何节点都可以生成事件并通过 CAN 广播它。对于这些事件,CAN 帧提
您好,我在解析 IIB Toolkit 中的任何 JSON 时遇到问题。 java计算节点抛出的异常为:java.lang.NoClassDefFoundError: org.json.JSONObj
我买了这个传感器: http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Weather/RHT03.pdf 输出为“MaxDetect 1-w
我正在用户空间编写包装器 API,用于在嵌入式 Linux 平台上用 C 语言控制 I2C 总线。我能够使用 read() 和 write() 方法以及 ioctl() 调用来选择从属设备,从而从传感
在我的软件(用 C++ 编写)中,我使用 Linux 标准函数打开 CAN 总线套接字并执行 I/O 操作。 套接字的打开和使用如下: /* Create the socket */ if ((
每天——大约 5 到 10 次——我的 USB 摄像头从系统中消失。它从第一天开始就发生了,因为制造商驱动程序与 Linux 不兼容。 lsusb 和 dmesg 一开始正确显示,但在较长时间后有时会
我分发了包含多个 Go 服务的应用程序。其中一些使用 Kafka 作为数据总线。我能够使用 Jaeger 的 opentracing 追踪服务之间的调用。我在图表上绘制 Kafka 跨度时遇到问题,它
我是一名优秀的程序员,十分优秀!