Vuex store with string: true not working

When I set strict: true , I get an 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 completely, it works fine. It is recommended to work in strict mode: what can I do to avoid errors? Because I don’t think that I mutate the store outside the mutation handlers in my code !?

See the behavior of this script .

+1
source share
1 answer

I don’t think that I mutate storage outside mutation handlers in my code

Although you do not change the state of the repository outside the 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 create an instance of the Map class, which 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, violating the vuex rule.

My suggestion is that you should create a vue component for the map, as others have said. Since the map instance has a link to the DOM element <div id="map"> , for me it refers to the view layer, not the storage layer. Vue is a presentation-level environment and focuses on everything related to the DOM, and Vuex is a state management library that is best suited for the serializable state of the application (like JSON data) and should be separated from the actual DOM elements.

As for communication with other components, if this is a direct parent-child relationship, you can use the details / child links and event callbacks; otherwise, you can extract / copy a specific state (for example, scaling) from the map instance and save it in the vuex repository or use the global event bus,

As a side note, vue does not guarantee that it will work well with any libral that modifies the DOM. The user who uses such a lib is responsible for managing it, and lifecycle hooks are the best place for such things.

+2
source

Source: https://habr.com/ru/post/1261803/


All Articles