Restrict vue/vuex reactivity

后端 未结 3 1831
忘了有多久
忘了有多久 2020-12-15 12:57

Let\'s assume we have some array of objects, and these objects never change. For example, that may be search results, received from google maps places api - every result is

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-15 13:40

    You can use Object.freeze() on those objects. This comes with a (really tiny!) performance hit, but it should be negligible if you don't add hundreds or thousands of objects at once.

    edit: Alternatively, you could freeze the array (much better performance) which will make Vue skip "reactifying" its contents.

    And when you need to add objects to that array, build a new one to replace the old one with:

    state.searchResults = Object.freeze(state.searchResults.concat([item]))
    

    That would be quite cheap even for bigger arrays.

提交回复
热议问题