Skip to main content

ion-route

路由组件采用一个组件,并在浏览器 URL 与 url 属性匹配时渲染它。

¥The route component takes a component and renders it when the Browser URL matches the url property.

注意

注意:该组件只能与普通和 Stencil JavaScript 项目一起使用。对于 Angular 项目,请使用 ion-router-outlet 和 Angular 路由。

¥Note: this component should only be used with vanilla and Stencil JavaScript projects. For Angular projects, use ion-router-outlet and the Angular router.

¥Navigation Hooks

导航钩子可用于执行任务或充当导航守卫。通过向每个 ion-route 上的 beforeEnterbeforeLeave 属性提供函数来使用钩子。返回 true 允许导航继续,而返回 false 则导致导航取消。返回 NavigationHookOptions 类型的对象允许你将导航重定向到另一个页面。

¥Navigation hooks can be used to perform tasks or act as navigation guards. Hooks are used by providing functions to the beforeEnter and beforeLeave properties on each ion-route. Returning true allows navigation to proceed, while returning false causes it to be cancelled. Returning an object of type NavigationHookOptions allows you to redirect navigation to another page.

接口

¥Interfaces

interface NavigationHookOptions {
/**

* A valid path to redirect navigation to.
*/
redirect: string;
}

用法

¥Usage

<ion-router>
<ion-route url="/home" component="page-home"></ion-route>
<ion-route url="/dashboard" component="page-dashboard"></ion-route>
<ion-route url="/new-message" component="page-new-message"></ion-route>
<ion-route url="/login" component="page-login"></ion-route>
</ion-router>
const dashboardPage = document.querySelector('ion-route[url="/dashboard"]');
dashboardPage.beforeEnter = isLoggedInGuard;

const newMessagePage = document.querySelector('ion-route[url="/dashboard"]');
newMessagePage.beforeLeave = hasUnsavedDataGuard;

const isLoggedInGuard = async () => {
const isLoggedIn = await UserData.isLoggedIn(); // Replace this with actual login validation

if (isLoggedIn) {
return true;
} else {
return { redirect: '/login' }; // If a user is not logged in, they will be redirected to the /login page
}
}

const hasUnsavedDataGuard = async () => {
const hasUnsavedData = await checkData(); // Replace this with actual validation

if (hasUnsavedData) {
return await confirmDiscardChanges();
} else {
return true;
}
}

const confirmDiscardChanges = async () => {
const alert = document.createElement('ion-alert');
alert.header = 'Discard Unsaved Changes?';
alert.message = 'Are you sure you want to leave? Any unsaved changed will be lost.';
alert.buttons = [
{
text: 'Cancel',
role: 'Cancel',
},
{
text: 'Discard',
role: 'destructive',
}
];

document.body.appendChild(alert);

await alert.present();

const { role } = await alert.onDidDismiss();

return (role === 'Cancel') ? false : true;
}

属性

¥Properties

beforeEnter

DescriptionA navigation hook that is fired when the route tries to enter. Returning true allows the navigation to proceed, while returning false causes it to be cancelled. Returning a NavigationHookOptions object causes the router to redirect to the path specified.
Attributeundefined
Type(() => NavigationHookResult | Promise<NavigationHookResult>) | undefined
Defaultundefined

beforeLeave

DescriptionA navigation hook that is fired when the route tries to leave. Returning true allows the navigation to proceed, while returning false causes it to be cancelled. Returning a NavigationHookOptions object causes the router to redirect to the path specified.
Attributeundefined
Type(() => NavigationHookResult | Promise<NavigationHookResult>) | undefined
Defaultundefined

component

DescriptionName of the component to load/select in the navigation outlet (ion-tabs, ion-nav) when the route matches.

The value of this property is not always the tagname of the component to load, in ion-tabs it actually refers to the name of the ion-tab to select.
Attributecomponent
Typestring
Defaultundefined

componentProps

DescriptionA key value { 'red': true, 'blue': 'white'} containing props that should be passed to the defined component when rendered.
Attributeundefined
Typeundefined | { [key: string]: any; }
Defaultundefined

url

DescriptionRelative path that needs to match in order for this route to apply.

Accepts paths similar to expressjs so that you can define parameters in the url /foo/:bar where bar would be available in incoming props.
Attributeurl
Typestring
Default''

事件

¥Events

NameDescriptionBubbles
ionRouteDataChangedUsed internally by ion-router to know when this route did change.true

方法

¥Methods

No public methods available for this component.

CSS 阴影部分

¥CSS Shadow Parts

No CSS shadow parts available for this component.

CSS 自定义属性

¥CSS Custom Properties

No CSS custom properties available for this component.

插槽

¥Slots

No slots available for this component.