我在vuex:中使用这个getter
authority_count (state, getters) {
return getters.dataView?.getUint16(4, true) || state.pack.authority_count;
}
当dataView为null时,我希望authority_count为state.pack.authority_count,并且在页面中加载文件后,我希望在用从文件加载的数据填充dataView后,在加载完成时重新计算authority_count。我怎样才能做到这一点?
问题是,当我让getters.dataView返回dataView时,authority_count仍然返回state.pack.authority_count.
使用文件输入从文件中获取二进制数据,并填充到arrayBuffer,由dataView getter访问。
加载阵列缓冲区
加载数据查看
登录authority_count
[HMR] Waiting for update signal from WDS...
getters.dataView: null
state.pack.authority_count: 5
链接项目中存在两个问题:
- 必须仅在Vuex突变中更改Vuex状态(例如
arrayBuffer
(:
mutations: {
setArrayBuffer(state, payload) {
state.arrayBuffer = payload;
}
},
并这样称呼它:
reader.onload = (event) => {
this.$store.commit('setArrayBuffer', event.target.result);
}
- 组件的
arrayBuffer
必须经过计算才能与Vuex保持同步:
data () {
return {
pack: this.$store.state.pack
}
},
computed: {
arrayBuffer() { // You could use `mapState` instead, same thing
return this.$store.state.arrayBuffer
},
buffer () {
return [...new Uint8Array(this.arrayBuffer)]
.map (b => b.toString(16).toUpperCase().padStart(2, '0'));
}
},
如果您希望pack
与Vuex同步,则需要对其执行相同的操作。