Subscribe on changes!

Even if there is a beforeUnmount hook, a warning of beforeDestroy appears.

avatar
Apr 27th 2021

Version

3.0.11

Reproduction link

https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHRlbXBsYXRlPlxuICA8aDE+e3sgbXNnIH19PC9oMT5cbjwvdGVtcGxhdGU+XG5cbjxzY3JpcHQ+XG4gIGNvbnN0IG1zZyA9ICdIZWxsbyBXb3JsZCEnXG4gIGV4cG9ydCBkZWZhdWx0IHtcbiAgICBzZXR1cCgpIHtcblx0XHRcdHJldHVybiB7XG5cdFx0XHQgIG1zZ1xuICAgICAgfTtcblx0XHR9LFxuICAgIGJlZm9yZURlc3Ryb3koKSB7XG4gICAgfSxcbiAgICBiZWZvcmVVbm1vdW50KCkge1xuICAgIH0sXG4gIH07XG48L3NjcmlwdD4ifQ==

Steps to reproduce

export default {
    beforeDestroy() {
    },
    beforeUnmount() {
    },
};

What is expected?

I use both beforeDestory and beforeUnmount to support Vue2 and Vue3 at the same time. If beforeUnmount has a hook, I hope that the beforeDestory warning does not appear.

What is actually happening?

beforeDestroy has been renamed to beforeUnmount.

avatar
Apr 27th 2021

as a workaround:

  import * as Vue from 'vue'
  const unmountKey = Vue.version > '3' ? 'beforeUnmount':'beforeDestroy'
  export default {
    [unmountKey]() {
      console.log(Vue.version)
    },
  };
avatar
Apr 27th 2021

@edison1105

Since there is only default in vue2

import Vue from "vue";

It is used as Vue.version.

If you use esm, a warning appears.

avatar
Apr 27th 2021

I recommend you to try this https://github.com/vueuse/vue-demi