Vuex store with quot;strict: truequot; does not work(带有“strict: true的 Vuex 商店不工作)
问题描述
When I set strict: true
I get error:
vue.js:525 [Vue warn]: Error in watcher "state"
(found in root instance)
warn @ vue.js:525
run @ vue.js:1752
update @ vue.js:1720
notify @ vue.js:584
reactiveSetter @ vue.js:814
Transform.resize @ transform.js:169
e.resize @ map.js:343
e._onWindowResize @ map.js:1204
vue.js:1756 Uncaught Error: [vuex] Do not mutate vuex store state outside mutation handlers.
at assert (vuex.js:182)
at Vue$3.store._vm.$watch.deep (vuex.js:714)
at Watcher.run (vue.js:1746)
at Watcher.update (vue.js:1720)
at Dep.notify (vue.js:584)
at Transform.reactiveSetter [as width] (vue.js:814)
at Transform.resize (transform.js:169)
at e.resize (map.js:343)
at e._onWindowResize (map.js:1204)
When I set strict: false
, or leave the line out completely, it works fine. As working in strict mode is recommended: what can I do to avoid the error? As I do not think I am mutating the store outside mutation handlers in my code!?
See behavior in this fiddle.
As I do not think I am mutating the store outside mutation handlers in my code
Although you are not mutating the store state outside mutation handlers, the mapboxgl.Map instance does.
var myMap = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v9',
hash: true,
center: [-74.0073, 40.7124],
zoom: 16
})
Here you are creating an instance of a Map class that has its own state (including a reference to the DOM element) and methods. This map instance will listen for DOM events (user interaction) and update its own state, thus breaking vuex's rule.
My suggestion is that you should create a vue component for the map as others said. Since the map instance has a reference to the DOM element <div id="map">
, to me it belongs to the view layer, not the store layer. Vue is a view layer framework and focuses on everything related to DOM, while Vuex is a state management library which is best for serializable application state (like JSON data) and it should be decoupled from actual DOM elements.
As for communication with other components, if it is direct parent-child relationship, you can use props/child refs and event callbacks; otherwise you can extract/copy certain state (e.g., zoom) from the map instance and save it to vuex store, or use a global event bus.
As a side note, vue comes with no guarantee that it will play well with any libraray that mutates the DOM. The user who uses such a lib is responsible for managing it, and the lifecycle hooks are the best place to do such things.
这篇关于带有“strict: true"的 Vuex 商店不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有“strict: true"的 Vuex 商店不工作
基础教程推荐
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 响应更改 div 大小保持纵横比 2022-01-01
- 动态更新多个选择框 2022-01-01