umijs / qiankun

📦 🚀 Blazing fast, simple and complete solution for micro frontends.
https://qiankun.umijs.org
MIT License
15.87k stars 2.02k forks source link

【bug】nuxt2接入qiankun,vuex使用出现了问题 #2797

Open meishishang opened 1 year ago

meishishang commented 1 year ago

What happens?

主应用使用Nuxt2,SPA模式 子应用1使用Nuxt2,SPA模式 子应用2使用Vue2

主应用加载子应用页面,nuxt子应用中使用了store.commit改变state数据,默认加载nuxt子应用,当我打开vue子应用,什么也不做再次切换回nuxt子应用时,触发store,commit,出现了以下报错 [vuex] do not mutate vuex store state outside mutation handlers. 但是我很确定我使用了mutation

录屏如下: ceshi1

Mini Showcase Repository(REQUIRED)

基础代码如下所示:

nuxt主应用: // ~/layouts/default.vue

<template>
  <div>
    <header class="title">nuxt qiankun main</header>
    <div class="menu-wrap">
      <ul>
        <li v-for="app in apps" :key="app.name">
          <nuxt-link :to="app.activeRule">{{app.name}}</nuxt-link>
        </li>
      </ul>
    </div>

    <div id="subapp"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import {
  registerMicroApps,
  start,
} from 'qiankun'
export default {
  mounted() {
    this.init()
  },
  computed: {
    ...mapState(['apps']),
  },
  methods: {
    async init() {
      // 注册所有子应用
      registerMicroApps(this.apps)

      // 启动
      start()
    },
  },
}
</script>

nuxt子应用: // ~/pages/default.vue

<template>
  <div class="nuxt-app">
    <header>
      {{name}}
      <h1 class="title">nuxt-sub-app</h1>
      <button @click="handleClick">修改值</button>
    </header>
    <Nuxt />
  </div>
</template>
<script>
import { mapState } from 'vuex'
export default {
  computed: {
    ...mapState(['name']),
  },
  mounted() {
    this.$store.commit('update', {
      name: 'ceshi'
    })
  },
  methods: {
    handleClick() {
      this.$store.commit('update', {
        name: new Date().getTime()
      })
    }
  }
}
</script>

// ~/store/index.js

export default {
  state: {
    name: 'aaa'
  },
  mutations: {
    update(state, data) {
      Object.keys(data).forEach(key => {
        state[key] = data[key]
      })
    },
  }
}

Context

有大佬遇到同样问题的吗?

spacekong commented 4 months ago

遇到了同样的问题,也是子应用二次加载时报错,这个问题你解决了么?

spacekong commented 4 months ago

遇到了同样的问题,也是子应用二次加载时报错,这个问题你解决了么?

我在子应用unmount时将store手动清除后问题解决 app.store?.replaceState({})