Skip to content

Commit

Permalink
feat: router add garfish instance
Browse files Browse the repository at this point in the history
  • Loading branch information
zhoushaw committed May 7, 2021
1 parent 2dd0702 commit bee506b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
35 changes: 34 additions & 1 deletion packages/runtime/router/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,40 @@ export const listenRouterAndReDirect = ({
listen();
};

const Router = {
export interface RouterInterface {
push: ({
path,
query,
}: {
path: string;
query?: {
[key: string]: string;
};
}) => void;
replace: ({
path,
query,
}: {
path: string;
query?: {
[key: string]: string;
};
}) => void;
beforeEach: (hook: RouterHook) => void;
afterEach: (hook: RouterHook) => void;
registerRouter: (Apps: Array<interfaces.AppInfo>) => void;
routerChange: (hook: RouterChange) => void;
listenRouterAndReDirect: ({
apps,
basename,
autoRefreshApp,
active,
deactive,
notMatch,
}: Options) => void;
}

const Router: RouterInterface = {
push,
replace,
beforeEach,
Expand Down
7 changes: 6 additions & 1 deletion packages/runtime/router/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import Garfish, { interfaces } from '@garfish/core';
import { assert, createKey, warn } from '@garfish/utils';
import { listenRouterAndReDirect } from './context';
import router, { listenRouterAndReDirect, RouterInterface } from './context';

interface Options {
autoRefreshApp?: boolean;
onNotMatchRouter?: (path: string) => Promise<void> | void;
}

declare module '@garfish/core' {
export default interface Garfish {
router: RouterInterface;
}

export namespace interfaces {
export interface AppInfo {
activeWhen?: string | ((path: string) => boolean); // 手动加载,可不填写路由
Expand All @@ -19,6 +23,7 @@ declare module '@garfish/core' {

export default function Router(args?: Options) {
return function (Garfish: Garfish): interfaces.Plugin {
Garfish.router = router;
return {
name: 'router',
bootstrap(options) {
Expand Down

0 comments on commit bee506b

Please sign in to comment.