返回介绍

Ionic4 路由组件 ion-route-redirect

发布于 2019-11-22 18:04:18 字数 4761 浏览 879 评论 0 收藏 0

Ionic4项目中我们可以使用Ionic4路由组件ion-route-redirect对项目进行布局。

ion-route-redirect官方文档地址:https://ionicframework.com/docs/api/route-redirect

A redirect router can only be used in the scope of ion-router as a direct children of it.

Note: this is only meant for vanilla JavaScript project. For Angular projects, use ion-router-outlet and the Angular router.

This route has only two configurable values:

  • from
  • to

Their meaning is obvious under the context of a redirection, that occurs from a given URL to another given URL.

In other for a redirection to occurs the from path needs to be an exact match of the navigated URL.

Redirection evaluation

An arbitrary number of redirect routes can be defined inside an ion-router, but only one can match.

Also, a redirection route WILL never redirect to another redirection router, since this could lead to infinity loops.

Let's say we have this two redirection rules:


  
  
</ion-router>

And the user navigates to /admin. The router will then redirect to /login and stop there.

It WILL NOT never evaluate more than one redirection rule in a roll.

Examples

Simple path

<ion-route-redirect from="/admin" to="/login">

This route will apply (redirect) when the user navigates to: /admin but it will NOT apply if the user navigates to /admin/posts.

In order to match any subpath of admin, the wildcard character (*) needs to be used.

<ion-route-redirect from="/admin/*" to="/login">

Redirect all routes to login

Redirection routes can work as guards, since that can prevent user to navigate to areas to your application based in a given condition, for example, if the user is authenticated or not.

{!this.isLoggedIn &&
   }

A router can be added and removed dynamically to redirect (or guard) some routes from being accessed.

Another approach is to modify the value of to, since given to the value of null or undefined makes disables the redirection.

 <ion-route-redirect from="*" to={this.isLoggedin ? undefined : '/login'}/>

ion-route-redirect 属性(Properties)

from

Description

A redirect route, redirects "from" a URL "to" another URL. This property is that "from" URL. It needs to be an exact match of the navigated URL in order to apply.

The path specified in this value is always an absolute path, even if the initial / slash is not specified.

Attributefrom
Typestring

to

Description

A redirect route, redirects "from" a URL "to" another URL. This property is that "to" URL. When the defined ion-route-redirect rule matches, the router will redirect to the path specified in this property.

The value of this property is always an absolute path inside the scope of routes defined in ion-router it can't be used with another router or to perform a redirection to a different domain.

Note that this is a virtual redirect, it will not cause a real browser refresh, again, it's a redirect inside the context of ion-router.

When this property is not specified or his value is undefined the whole redirect route is noop, even if the "from" value matches.

Attributeto
Typenull | string | undefined

ion-route-redirect 事件(Events)

NameDescription
ionRouteRedirectChangedInternal event that fires when any value of this rule is added/removed from the DOM, or any of his public properties changes. `ion-router` captures this event in order to update his internal registry of router rules.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文