生命周期钩子在从Vue2迁移到Vue3后无法正常工作。

huangapple go评论57阅读模式
英文:

Lifecycle hooks not working properly in order After migration from vu2 to vue3

问题

问题出在我从“路线1”切换到“路线2”时,首先调用“路线2”的“created”,然后才是“路线1”的“unmounted”。实际上应该是首先调用“路线1”的“unmounted”,然后才是“路线1”的“created”。

我在两个路线中使用了一个共同的存储,并在所有路线的“created”和“unmounted”中注册和注销这些存储。

路线1 -

created() {
this.registerStore('Store1', Store1);
}
unmounted() {
this.unregisterStore('Store1');
}

这里的Store1是Route2中的一个共同存储。

路线2 -

created() {
this.registerStore('Store2', Store2);
this.registerStore('Store1', Store1);
}
unmounted() {
this.unregisterStore('Store2');
this.unregisterStore('Store1');
}

现在的问题是,当我从“路线1”切换到“路线2”时,首先调用“路线2”的“created”,然后才是“路线1”的“unmounted”。因此,在这种情况下,我的Store1首先注册,然后注销,因此导致我在使用Store1的Route2上出现错误。

因此,在这种情况下,我无法使用共同的存储,而需要使用分开的存储。

英文:

The issue is when I switch from Route 1 to Route 2 then First "created" of route2 being called after that "unmounted" of route1. Actually it should be like first "unmounted" of Route1 should call after that "created" of Route1

I am using a common store in two routes. And registering and deregistering those stores in "created" and "unmounted" in all routes.

Route 1-

created() {
this.registerStore('Store1', Store1);
}
unmounted() {
this.unregisterStore('Store1');
}

Here Store1 is a common store in Route2.

Route 2 -

created() {
this.registerStore('Store2', Store2);
this.registerStore('Store1', Store1);
}
unmounted() {
this.unregisterStore('Store2');
this.unregisterStore('Store1');
}

Now the issue is when I switch from Route 1 to Route 2 then First "created" of route2 being called after that "unmounted" of route1. So in this case My store1 first register and then unregister because of that my Page is getting error on Route2 where using Store1

So I am not able to use common store in that case and separated stores.

答案1

得分: 1

这里是您要翻译的内容:

It's my observation that a new component's created hook runs before an old component's unmounted hook in Vue 2, same as Vue 3, and is also backed up by this older answer on SO.

Regardless, this type of logic should be centralized so conflicts of this type can be avoided. Something like the beforeEnter guard or beforeEach guard could unregister and register stores as needed based on the component you're leaving from and the component you're entering to

beforeEnter example:

{
    path: '/route2',
    component: Component2,
    beforeEnter: (to, from) => {
      if (from.path === '/route1') {
        unregisterStore('Store1');
      }
      registerStore('Store2', Store2);
      registerStore('Store1', Store1);
      // accept the navigation
      return true
    },
},

Logic can perhaps be simplified (not sure if unregistering then re-registering immediately is necessary for your use case).

英文:

It's my observation that a new component's created hook runs before an old component's unmounted hook in Vue 2, same as Vue 3, and is also backed up by this older answer on SO.

Regardless, this type of logic should be centralized so conflicts of this type can be avoided. Something like the beforeEnter guard or beforeEach guard could unregister and register stores as needed based on the component you're leaving from and the component you're entering to

beforeEnter example:

{
    path: '/route2',
    component: Component2,
    beforeEnter: (to, from) => {
      if (from.path === '/route1') {
        unregisterStore('Store1');
      }
      registerStore('Store2', Store2);
      registerStore('Store1', Store1);
      // accept the navigation
      return true
    },
},

Logic can perhaps be simplified (not sure if unregistering then re-registering immediately is necessary for your use case).

huangapple
  • 本文由 发表于 2023年6月13日 02:12:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459266.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定