简体   繁体   English

在 Nuxt 商店(vuex)中使用 window.open

[英]Use window.open in a Nuxt store (vuex)

I'm using Nuxt.js and I want to open a window modal using window.open() in a Vuex store.我正在使用 Nuxt.js 并且我想在 Vuex 商店中使用window.open()打开 window 模态。

This is my store code:这是我的商店代码:

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
    }
  }
}

The problem is that when I call $store.commit('store-name/openPopup', item.url) , I get the below error:问题是当我调用$store.commit('store-name/openPopup', item.url)时,我收到以下错误:

(I call that function with v-on:click in a element generate with v-for and every item has a unique url) (我称之为 function 与v-on:click在一个元素中使用v-for生成,每个项目都有一个唯一的 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

Does anyone know the cause of this error and how to fix it?有谁知道这个错误的原因以及如何解决它?

Either you can think of an another way to solve your problem or add this to your Vuex.您可以考虑另一种方法来解决您的问题,或者将其添加到您的 Vuex 中。

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

This disables Vuex's strict mode .这会禁用 Vuex 的严格模式 That means Vuex state can be mutated outside of mutations.这意味着 Vuex state 可以在突变之外进行突变。
Note: Nuxt.js automatically disables strict mode in production but keeps it enabled in developer mode.注意: Nuxt.js 在生产中自动禁用严格模式,但在开发者模式下保持启用。 If you add this line of code to your code, it disables strict mode in developer mode.如果您将这行代码添加到您的代码中,它会在开发人员模式下禁用严格模式。 So it's safe.所以是安全的。

Sidenote: this line of code won't do anything:旁注:这行代码不会做任何事情:

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

Why?为什么? Because this.closePopup is not a function.因为this.closePopup不是 function。 So the closePopup function won't be called if the beforeunload event occurs.因此,如果发生beforeunload事件,将不会调用closePopup function。 To fix that, replace that line of code with this:要解决此问题,请将那行代码替换为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM