发布于 4年前

vuex如何设置state里面的属性全部为null?

state里面很多属性 我想在某个mutation中把state里面的值全部设置为null 如果一个个去这样this.state.routeName=null设置太麻烦了 因为state里面的属性太多了 有什么简单的办法吗?

mutation.js

setNull () {

}

state.js

export default {
  routeName: '',
  auth: 1,
  list: ['a', 'b', 'c'],
  navShow: true,
  files: null
}

解决方案:

setNull: (state) => {
  Object.keys(state).forEach(e => {
     state[e] = null
  })
}
©2020 edoou.com   京ICP备16001874号-3