Skip to content

Commit

Permalink
feat(omi-router): fix redirection issue, support base path setting
Browse files Browse the repository at this point in the history
  • Loading branch information
dntzhang committed Jan 8, 2024
1 parent 93cc195 commit a6b9bfd
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- 📶 信号 **Signal** 驱动的响应式编程
-**微小的**尺寸,**极速的**性能
- 🎉 [官方 OMIU 源码](https://github.com/Tencent/omi/tree/master/packages/omiu) 进行中.. & [OMIU 预览](https://omi.cdn-go.cn/omiu/latest/)
- 🐲 [OMI Form](https://omi.cdn-go.cn/form/latest/docs/) & [OMI Form 游乐场](https://omi.cdn-go.cn/form/latest/play/)
- 🌐 你要的一切都有: **Web Components**, **JSX**, Router, Suspense, Directive, Tailwindcss...
- 💯 面向对象编程(OOP) 和 数据驱动编程(DOP) 两种范式都支持
- 💒 使用 **Constructable Stylesheets** 轻松管理和共享样式
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ English | [简体中文](./README.CN.md)

- 📶 **Signal**-driven reactive programming
- 🎉 [OMIU Preview](https://omi.cdn-go.cn/omiu/latest/) in progress & [OMIU Source Code](https://github.com/Tencent/omi/tree/master/packages/omiu)
- 🐲 [OMI Form](https://omi.cdn-go.cn/form/latest/docs/) & [OMI Form Playground](https://omi.cdn-go.cn/form/latest/play/)
-**Tiny** size, **Fast** performance
- 🌐 Everything you need: **Web Components**, **JSX**, Router, Suspense, Directive, Tailwindcss...
- 💯 Both **object** oriented programming(OOP) and **data** oriented programming(DOP) are supported
Expand Down
2 changes: 1 addition & 1 deletion packages/omi-router/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "omi-router",
"version": "4.1.5",
"version": "4.1.6",
"description": "Router for Omi",
"main": "dist/index.es.js",
"exports": {
Expand Down
13 changes: 10 additions & 3 deletions packages/omi-router/src/router-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface Route {

interface Props {
routes: Route[];
base?: string;
}

@tag('router-view')
Expand All @@ -32,12 +33,14 @@ export class RouterView extends Component<Props> {
params: Record<string, unknown> = {}
query: Record<string, unknown> = {}
hash: string = ''
base: string = ''

install() {
// 修改组件内部的 this.router 为当前 router-view 的实例
mixin({
router: this
})
this.base = this.props.base || ''

this.routes = this.props.routes.map(route => {
const keys: Key[] = []
Expand Down Expand Up @@ -104,9 +107,13 @@ export class RouterView extends Component<Props> {
for (const route of this.routes) {
const match = route.regex?.exec(path)
if (match) {
if(route.redirect) {
const newPath = this.isHashMode ? `#${route.redirect}` : route.redirect;
window.location.href = window.location.origin + newPath;
if (route.redirect) {
if( this.isHashMode) {
window.location.hash = route.redirect
} else {
const newPath = this.base + route.redirect;
window.location.href = window.location.origin + newPath;
}
return;
}
if (route.beforeEnter) {
Expand Down
3 changes: 2 additions & 1 deletion packages/omi-router/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ type CSSItem = CSSStyleSheet | Module | string
export class Router {
el: RouterView

constructor(options: { routes: Route[]; renderTo: string, css?: CSSItem[] }) {
constructor(options: { routes: Route[]; renderTo: string, css?: CSSItem[], base?: string }) {
this.el = render((
<router-view
onInstall={(evt: CustomEvent) => {
// @ts-ignore
evt.detail.constructor.css = options.css
}}
routes={options.routes}
base={options.base}
/>
), options.renderTo)
}
Expand Down
1 change: 1 addition & 0 deletions packages/omi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

- 📶 **Signal**-driven reactive programming
- 🎉 [OMIU Preview](https://omi.cdn-go.cn/omiu/latest/) in progress & [OMIU Source Code](https://github.com/Tencent/omi/tree/master/packages/omiu)
- 🐲 [OMI Form](https://omi.cdn-go.cn/form/latest/docs/) & [OMI Form Playground](https://omi.cdn-go.cn/form/latest/play/)
-**Tiny** size, **Fast** performance
- 🌐 Everything you need: **Web Components**, **JSX**, Router, Suspense, Directive, Tailwindcss...
- 💯 Both **object** oriented programming(OOP) and **data** oriented programming(DOP) are supported
Expand Down

0 comments on commit a6b9bfd

Please sign in to comment.