Skip to content

Commit af48850

Browse files
committed
fix(module-loader): router.beforeEach in beforeCreate
affects: @vue-async/module-loader router.beforeEach is not working well in beforeCreate at first time, move to Vue.prototype._init
1 parent 5c9c479 commit af48850

File tree

14 files changed

+3211
-65
lines changed

14 files changed

+3211
-65
lines changed

packages/module-loader/dev/App.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import Playground from './Playground';
2-
import DynamicComponent from './DynamicComponent';
1+
// app
32

43
export default Vue.extend({
54
name: 'App',
6-
created() {
7-
this.$dynamicComponent.add(DynamicComponent, 'dynamic');
8-
},
95
render(h) {
10-
return h('div', { domProps: { id: 'app' } }, [h(Playground)]);
6+
return h('div', { domProps: { id: 'app' } }, [
7+
h('router-link', { staticStyle: { margin: '0 10px; 5px' }, props: { to: { name: 'remote-page-a' } } }, 'Page A'),
8+
h('router-view'),
9+
]);
1110
},
1211
});
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
export default Vue.extend({
22
name: 'Playground',
3-
computed: Vuex.mapState('dynamicComponent', { dynamicComponents: 'dynamic' }),
3+
computed: Vuex.mapState('dynamicComponent', { dynamicComponents: 'dashboard' }),
44
render(h) {
5-
const children = Object.values(this.dynamicComponents || {}).map((component) => h(component));
6-
return h('div', { staticClass: 'playground' }, [h('h3', 'PLAYGROUND'), ...children]);
5+
const children = Object.values(this.dynamicComponents || {}).map((component) =>
6+
h(component.component || component),
7+
);
8+
return h('div', { staticClass: 'playground' }, [h('h3', 'Dashboard'), ...children]);
79
},
810
});

packages/module-loader/dev/DynamicComponent.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/module-loader/dev/main.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import Vue from 'vue';
2-
import router from './router';
2+
import router, { addRoutes } from './router';
33
import store from './store';
44
import ModuleLoader from '@vue-async/module-loader';
55
import App from './app';
66

7-
const moduleLoader = new ModuleLoader({});
7+
const moduleLoader = new ModuleLoader({
8+
addRoutes, // 重写 addRoutes
9+
});
810

911
const vm = new Vue({
10-
data: () => ({ isLoaded: document.readyState === 'complete' }),
1112
router,
1213
store,
1314
moduleLoader,
14-
render(h) {
15-
return this.isLoaded ? h(App) : undefined;
16-
},
17-
}).$mount('#app');
15+
render: (h) => h(App),
16+
});
1817

19-
// Prevent layout jump while waiting for styles
20-
vm.isLoaded ||
21-
window.addEventListener('load', () => {
22-
vm.isLoaded = true;
23-
});
18+
vm.$moduleLoader({
19+
dymanicRouter: '/static/dymanicRouter.umd.js',
20+
dymanicComponent: '/static/dymanicComponent.umd.js',
21+
}).then(() => {
22+
vm.$mount('#app');
23+
});
Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
11
/**
22
* router
33
*/
4-
const component1 = {
5-
template: `<div class="title">Page 1</div>`,
6-
};
7-
const notfound = {
8-
template: `<div class="title">Not Found</div>`,
4+
import Dashboard from './Dashboard';
5+
const Notfound = {
6+
template: `<h3>Not Found</h3>`,
97
};
108

119
const router = new VueRouter({
10+
base: '/',
11+
mode: 'history',
1212
routes: [
1313
{
14-
path: '/page1',
15-
name: 'Page 1',
16-
component: component1,
14+
path: '/',
15+
name: 'index',
16+
component: Dashboard,
1717
},
18-
{ path: '*', component: notfound },
18+
{ path: '*', component: Notfound },
1919
],
2020
});
2121

22+
// 模块中路由配置没有根前缀,用于主程序自定义
23+
function root(routes) {
24+
return routes.map((route) => {
25+
route.path = '/' + route.path;
26+
return route;
27+
});
28+
}
29+
30+
export function addRoutes(routes) {
31+
const options = router.options;
32+
// 合并路由
33+
options.routes = root(routes).concat(options.routes);
34+
const newRouter = new VueRouter(options);
35+
router.matcher = newRouter.matcher;
36+
}
37+
2238
export default router;

0 commit comments

Comments
 (0)