【问题标题】:Use window.open in a Nuxt store (vuex)在 Nuxt 商店(vuex)中使用 window.open
【发布时间】:2021-07-22 14:32:45
【问题描述】:

我正在使用 Nuxt.js,我想在 Vuex 商店中使用 window.open() 打开一个窗口模式。

这是我的商店代码:

export const state = () => ({
  popup: null
})

export const mutations = {
  openPopup (state, url) {
    state.popup = window.open(url, '', 'width=500,height=776')
    state.popup.addEventListener('beforeunload', this.closePopup)
  },
  closePopup (state) {
    if (state.popup) {
      state.popup.close()
      state.popup = null
    }
  }
}

问题是当我调用$store.commit('store-name/openPopup', item.url) 时,我收到以下错误:

(我在使用v-for 生成的元素中使用v-on:click 调用该函数,并且每个项目都有一个唯一的url)

RangeError: Maximum call stack size exceeded
    at _traverse (webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:2122)
    at _traverse (webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:2122)
    at _traverse (webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:2122)
    at _traverse (webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:2122)
    at _traverse (webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:2122)
    at _traverse (webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:2122)
    at _traverse (webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:2122)
    at _traverse (webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:2122)
    at _traverse (webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:2122)
    at _traverse (webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:2122)
_callee$ @ client.js?06a0:103

有谁知道这个错误的原因以及如何解决它?

【问题讨论】:

    标签: javascript node.js vue.js nuxt.js vuex


    【解决方案1】:

    您可以考虑另一种方法来解决您的问题,或者将其添加到您的 Vuex。

    // store/index.js
    export const strict = false
    

    这会禁用 Vuex 的 strict mode。这意味着 Vuex 状态可以在突变之外发生突变。
    注意: Nuxt.js 在生产环境中自动禁用严格模式,但在开发者模式中保持启用状态。如果您将这行代码添加到您的代码中,它会在开发人员模式下禁用严格模式。所以是安全的。

    旁注:这行代码不会做任何事情:

    state.popup.addEventListener('beforeunload', this.closePopup)
    

    为什么?因为this.closePopup 不是函数。所以如果beforeunload 事件发生,closePopup 函数将不会被调用。要解决这个问题,请将那行代码替换为:

    state.popup.addEventListener('beforeunload', this._mutations.closePopup[0])
    

    【讨论】:

    • 我已经读到,当您在商店中进行大操作时,严格模式会变得“繁重”,而我没有。那么我可以在生产中使用它而不会产生负面影响吗?
    • @alex3025 我弄错了。这与我写的相反。不建议在生产环境中启用严格模式。此外,Nuxt.js 会在生产环境中自动禁用严格模式,因此无需担心。只需将export const strict = false 添加到您的index.js
    • 但是当我投入生产时,问题会出现?
    • @alex3025 很高兴我能帮上忙!
    • @alex3025 我很高兴它成功了。你现在可以接受答案了:)
    猜你喜欢
    • 1970-01-01
    • 2018-11-23
    • 2018-12-19
    • 2020-08-25
    • 2020-12-07
    • 2019-11-04
    • 1970-01-01
    • 2019-03-23
    • 2017-12-18
    相关资源
    最近更新 更多