Skip to content

Commit 9d8855a

Browse files
committed
fix: remove deprecated lifecycle hooks
- rename `onBeforeDestroy` to `onBeforeUnmount` - remove `onCreated` - remove `onDestroyed`
1 parent ae44409 commit 9d8855a

File tree

4 files changed

+24
-81
lines changed

4 files changed

+24
-81
lines changed

src/apis/lifecycle.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,14 @@ function injectHookOption(Vue: VueConstructor, vm: ComponentInstance, hook: stri
2727
options[hook] = mergeFn(options[hook], val);
2828
}
2929

30-
export const onCreated = createLifeCycle('created');
30+
// export const onCreated = createLifeCycle('created');
3131
export const onBeforeMount = createLifeCycle('beforeMount');
3232
export const onMounted = createLifeCycle('mounted');
3333
export const onBeforeUpdate = createLifeCycle('beforeUpdate');
3434
export const onUpdated = createLifeCycle('updated');
35-
export const onActivated = createLifeCycle('activated');
36-
export const onDeactivated = createLifeCycle('deactivated');
37-
export const onBeforeDestroy = createLifeCycle('beforeDestroy');
38-
export const onDestroyed = createLifeCycle('destroyed');
39-
export const onErrorCaptured = createLifeCycle('errorCaptured');
40-
35+
export const onBeforeUnmount = createLifeCycle('beforeDestroy');
4136
// only one event will be fired between destroyed and deactivated when an unmount occurs
4237
export const onUnmounted = createLifeCycles(['destroyed', 'deactivated'], genName('unmounted'));
38+
export const onErrorCaptured = createLifeCycle('errorCaptured');
39+
export const onActivated = createLifeCycle('activated');
40+
export const onDeactivated = createLifeCycle('deactivated');

src/setup.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { VueConstructor } from 'vue';
22
import { ComponentInstance, SetupContext, SetupFunction, Data } from './component';
33
import { Ref, isRef, isReactive, nonReactive } from './reactivity';
44
import { getCurrentVM, setCurrentVM } from './runtimeContext';
5-
import { hasOwn, isPlainObject, assert, proxy, warn, logError, isFunction } from './utils';
5+
import { hasOwn, isPlainObject, assert, proxy, warn, isFunction } from './utils';
66
import { ref } from './apis/state';
77
import vmStateManager from './vmStateManager';
88

@@ -135,13 +135,9 @@ export function mixin(Vue: VueConstructor) {
135135
const setup = vm.$options.setup!;
136136
const ctx = createSetupContext(vm);
137137
let binding: ReturnType<SetupFunction<Data, Data>> | undefined | null;
138-
activateCurrentInstance(
139-
vm,
140-
() => {
141-
binding = setup(props, ctx);
142-
},
143-
err => logError(err, vm, 'setup()')
144-
);
138+
activateCurrentInstance(vm, () => {
139+
binding = setup(props, ctx);
140+
});
145141

146142
if (!binding) return;
147143

test/apis/lifecycle.spec.js

Lines changed: 12 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,15 @@
11
const Vue = require('vue/dist/vue.common.js');
22
const {
3-
onCreated,
43
onBeforeMount,
54
onMounted,
65
onBeforeUpdate,
76
onUpdated,
8-
onBeforeDestroy,
9-
onDestroyed,
7+
onBeforeUnmount,
8+
onUnmounted,
109
onErrorCaptured,
1110
} = require('../../src');
1211

1312
describe('Hooks lifecycle', () => {
14-
describe('created', () => {
15-
it('work with created option', () => {
16-
const spy = jest.fn();
17-
new Vue({
18-
created() {
19-
spy('option');
20-
},
21-
setup() {
22-
onCreated(() => spy('hook'));
23-
},
24-
});
25-
expect(spy.mock.calls.length).toBe(2);
26-
expect(spy).toHaveBeenNthCalledWith(1, 'option');
27-
expect(spy).toHaveBeenNthCalledWith(2, 'hook');
28-
});
29-
30-
it('can register multiple callbacks', () => {
31-
const spy = jest.fn();
32-
new Vue({
33-
setup() {
34-
onCreated(() => spy('first'));
35-
onCreated(() => spy('second'));
36-
},
37-
});
38-
expect(spy.mock.calls.length).toBe(2);
39-
expect(spy).toHaveBeenNthCalledWith(1, 'first');
40-
expect(spy).toHaveBeenNthCalledWith(2, 'second');
41-
});
42-
43-
it('should have completed observation', () => {
44-
const spy = jest.fn();
45-
new Vue({
46-
data() {
47-
return {
48-
a: 1,
49-
};
50-
},
51-
setup(_, { _vm }) {
52-
onCreated(() => {
53-
expect(_vm.a).toBe(1);
54-
spy();
55-
});
56-
},
57-
});
58-
expect(spy).toHaveBeenCalled();
59-
});
60-
});
61-
6213
describe('beforeMount', () => {
6314
it('should not have mounted', () => {
6415
const spy = jest.fn();
@@ -198,7 +149,7 @@ describe('Hooks lifecycle', () => {
198149
props: ['todo'],
199150
setup() {
200151
onBeforeUpdate(beforeUpdate);
201-
onDestroyed(destroyed);
152+
onUnmounted(destroyed);
202153
},
203154
});
204155

@@ -290,7 +241,7 @@ describe('Hooks lifecycle', () => {
290241
props: ['todo'],
291242
setup() {
292243
onUpdated(updated);
293-
onDestroyed(destroyed);
244+
onUnmounted(destroyed);
294245
},
295246
});
296247

@@ -320,13 +271,13 @@ describe('Hooks lifecycle', () => {
320271
});
321272
});
322273

323-
describe('beforeDestroy', () => {
274+
describe('beforeUnmount', () => {
324275
it('should be called before destroy', () => {
325276
const spy = jest.fn();
326277
const vm = new Vue({
327278
render() {},
328279
setup(_, { _vm }) {
329-
onBeforeDestroy(() => {
280+
onBeforeUnmount(() => {
330281
expect(_vm._isBeingDestroyed).toBe(false);
331282
expect(_vm._isDestroyed).toBe(false);
332283
spy();
@@ -341,13 +292,13 @@ describe('Hooks lifecycle', () => {
341292
});
342293
});
343294

344-
describe('destroyed', () => {
295+
describe('unmounted', () => {
345296
it('should be called after destroy', () => {
346297
const spy = jest.fn();
347298
const vm = new Vue({
348299
render() {},
349300
setup(_, { _vm }) {
350-
onDestroyed(() => {
301+
onUnmounted(() => {
351302
expect(_vm._isBeingDestroyed).toBe(true);
352303
expect(_vm._isDestroyed).toBe(true);
353304
spy();
@@ -381,10 +332,8 @@ describe('Hooks lifecycle', () => {
381332
const Child = {
382333
setup(_, { _vm }) {
383334
child = _vm;
384-
onCreated(() => {
385-
err = new Error('child');
386-
throw err;
387-
});
335+
err = new Error('child');
336+
throw err;
388337
},
389338
render() {},
390339
};
@@ -396,9 +345,9 @@ describe('Hooks lifecycle', () => {
396345
render: h => h(Child),
397346
}).$mount();
398347

399-
expect(spy).toHaveBeenCalledWith(err, child, 'created hook');
348+
expect(spy).toHaveBeenCalledWith(err, child, 'data()');
400349
// should propagate by default
401-
expect(globalSpy).toHaveBeenCalledWith(err, child, 'created hook');
350+
expect(globalSpy).toHaveBeenCalledWith(err, child, 'data()');
402351
});
403352
});
404353
});

test/setup.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const Vue = require('vue/dist/vue.common.js');
2-
const { ref, computed, onCreated, createElement: h } = require('../src');
2+
const { ref, computed, createElement: h } = require('../src');
33

44
describe('setup', () => {
55
beforeEach(() => {
@@ -301,11 +301,11 @@ describe('setup', () => {
301301
setup() {
302302
new Vue({
303303
setup() {
304-
onCreated(() => spy(1));
304+
spy(1);
305305
},
306306
});
307307

308-
onCreated(() => spy(2));
308+
spy(2);
309309
},
310310
});
311311
expect(spy.mock.calls.length).toBe(2);

0 commit comments

Comments
 (0)