Skip to content

Commit 431a890

Browse files
committed
feat: Meta dispatcher should work even if store is initialized later on
1 parent 448d3e7 commit 431a890

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

src/router-dispatcher-service.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ import { RouterService } from "./router-service";
99
*/
1010
export class RouterDispatcherService {
1111
/**
12-
* Current store
12+
* Current Vue instance
1313
*/
14-
public store: any;
14+
public vm: any;
1515

1616
/**
1717
* Constructor for the RouterDispatcherService
1818
*
19-
* @param {store} store The app store
19+
* @param {Vue} vm Vue Instance
2020
*/
21-
public constructor(store: any) {
22-
this.store = store;
21+
public constructor(vm: any) {
22+
this.vm = vm;
2323
}
2424

2525
/**
@@ -29,7 +29,13 @@ export class RouterDispatcherService {
2929
* @returns {boolean} If has finished dispatch or not
3030
*/
3131
public dispatchFromMeta(meta: any): boolean {
32-
if (!meta || !this.store) {
32+
if (!meta || !this.vm) {
33+
return false;
34+
}
35+
36+
if (!this.vm.$store) {
37+
console.error('META DISPATCHER', 'Store not found');
38+
3339
return false;
3440
}
3541

@@ -48,22 +54,22 @@ export class RouterDispatcherService {
4854
* @returns {Promise} Dispatched action result
4955
*/
5056
public dispatch(type: string, payload: unknown): Promise<void> {
51-
return this.store.dispatch(type, payload);
57+
return this.vm.$store.dispatch(type, payload);
5258
}
5359
}
5460

5561
/**
5662
* Registers generic action dispatcher
5763
*
58-
* @param {RouterService} router Vue-Router compatible class
59-
* @param {*} store Vuex Store Instance, or any other store that has a dispatch method exposed, with a type as 1st arg and payload as 2nd
64+
* @param {RouterService} router Vue-Router compatible class
65+
* @param {*} vm Vue Instance
6066
* @returns {void}
6167
*/
6268
export const registerActionDispatcher = (
6369
router: RouterService,
64-
store: any
70+
vm: any
6571
): void => {
66-
const dispatcherService = new RouterDispatcherService(store);
72+
const dispatcherService = new RouterDispatcherService(vm);
6773

6874
router.beforeResolve((to: Route, _from: Route, _next: NextContext) => {
6975
const route = router.getRoute(to);

src/vue-router.common.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,7 @@ export const createRouter = (
6060
vm.mixin(routerMixin);
6161
}
6262

63-
// Register Action Dispatcher if store is available
64-
if (proto.$store) {
65-
registerActionDispatcher(router, proto.$store);
66-
}
63+
registerActionDispatcher(router, proto);
6764

6865
return router;
6966
};

0 commit comments

Comments
 (0)