fix: preset-build-in的route api 兼容

This commit is contained in:
wanchun 2022-06-06 18:02:24 +08:00
parent 7c689db1aa
commit 00effaaa53
2 changed files with 24 additions and 4 deletions

View File

@ -328,7 +328,7 @@ export default function (api) {
api.addCoreExports(() => [ api.addCoreExports(() => [
{ {
specifiers: ['getRoutes', 'defineRouteMeta'], specifiers: ['getRoutes', 'getRouter', 'getHistory', 'destroyRouter', 'defineRouteMeta'],
source: absCoreFilePath source: absCoreFilePath
} }
]); ]);

View File

@ -7,7 +7,8 @@ export function getRoutes() {
} }
const ROUTER_BASE = '{{{ routerBase }}}'; const ROUTER_BASE = '{{{ routerBase }}}';
let router = null;
let history = null;
export const createRouter = (routes) => { export const createRouter = (routes) => {
const createHistory = plugin.applyPlugins({ const createHistory = plugin.applyPlugins({
key: 'modifyCreateHistory', key: 'modifyCreateHistory',
@ -17,14 +18,14 @@ export const createRouter = (routes) => {
}, },
initialValue: {{{ CREATE_HISTORY }}}, initialValue: {{{ CREATE_HISTORY }}},
}); });
const history = createHistory(ROUTER_BASE); history = createHistory(ROUTER_BASE);
// 修改routes // 修改routes
plugin.applyPlugins({ plugin.applyPlugins({
key: 'patchRoutes', key: 'patchRoutes',
type: ApplyPluginsType.event, type: ApplyPluginsType.event,
args: { routes }, args: { routes },
}); });
const router = createVueRouter({ router = createVueRouter({
history, history,
routes routes
}); });
@ -38,6 +39,25 @@ export const createRouter = (routes) => {
return router; return router;
}; };
export const getRouter = ()=>{
if(!router){
console.warn(`[preset-build-in] router is null`)
}
return router;
}
export const getHistory = ()=>{
if(!history){
console.warn(`[preset-build-in] history is null`)
}
return history;
}
export const destroyRouter = ()=>{
router = null;
history = null;
}
export const defineRouteMeta = (param)=>{ export const defineRouteMeta = (param)=>{
return param return param
} }