Vue 导航
本指南介绍了路由如何在使用 Ionic 和 Vue 构建的应用中工作。
¥This guide covers how routing works in an app built with Ionic and Vue.
IonRouterOutlet
组件在底层使用了流行的 Vue 路由 库。使用 Ionic 和 Vue Router,你可以创建具有丰富页面过渡的多页面应用。
¥The IonRouterOutlet
component uses the popular Vue Router library under the hood. With Ionic and Vue Router, you can create multi-page apps with rich page transitions.
你所了解的有关使用 Vue Router 进行路由的所有知识都会延续到 Ionic Vue 中。让我们看一下 Ionic Vue 应用的基础知识以及路由如何与其配合使用。
¥Everything you know about routing using Vue Router carries over into Ionic Vue. Let's take a look at the basics of an Ionic Vue app and how routing works with it.
简要说明
¥A Brief Note
在阅读本指南时,你可能会注意到大多数概念与不带 Ionic Framework 的 Vue Router 中的概念非常相似。你的观察是正确的!Ionic Vue 利用 Vue Router 的最佳部分,尽可能无缝地过渡到使用 Ionic Framework 构建应用。因此,我们建议尽可能依赖 Vue Router 功能,而不是尝试构建自己的路由解决方案。
¥While reading this guide, you may notice that most of these concepts are very similar to the concepts found in Vue Router without Ionic Framework. Your observation would be correct! Ionic Vue leverages the best parts of Vue Router to make the transition to building apps with Ionic Framework as seamless as possible. As a result, we recommend relying on Vue Router features as much as possible rather than trying to build your own routing solutions.
简单的路由
¥A Simple Route
以下是定义到 "/home" URL 的单个路由的示例路由配置。当你访问 "/home" 时,路由会渲染 HomePage
组件。
¥Here is a sample routing configuration that defines a single route to the "/home" URL. When you visit "/home", the route renders the HomePage
component.
router/index.ts
import { createRouter, createWebHistory } from '@ionic/vue-router';
import { RouteRecordRaw } from 'vue-router';
import HomePage from '@/views/Home.vue';
const routes: Array<RouteRecordRaw> = [
{
path: '/',
name: 'Home',
component: HomePage,
},
];
const router = createRouter({
history: createWebHistory(process.env.BASE_URL),
routes,
});
export default router;
在应用初始加载时,应用将渲染 HomePage
组件,因为这是此处配置的组件。
¥On the app's initial load, the app will render the HomePage
component as that is what is configured here.
处理重定向
¥Handling Redirects
如果我们想在初始负载上选择不同的路径怎么办?为此,我们可以使用路由重定向。重定向的工作方式与典型的路由对象相同,但仅包含一些不同的键:
¥What if we wanted to land a different path on our initial load? For this, we can use router redirects. Redirects work the same way that a typical route object does, but just includes some different keys:
const routes: Array<RouteRecordRaw> = [
{
path: '/',
redirect: '/home',
},
{
path: '/home',
name: 'Home',
component: HomePage,
},
];
在重定向中,我们查找应用的索引路径。然后,如果我们加载它,我们就会重定向到 home
路由。
¥In our redirect, we look for the index path of our app. Then if we load that, we redirect to the home
route.
导航到不同的路由
¥Navigating to Different Routes
这一切都很棒,但是如何实际导航到路由呢?为此,我们可以使用 router-link
属性。让我们创建一个新的路由设置:
¥This is all great, but how does one actually navigate to a route? For this, we can use the router-link
property. Let's create a new routing setup:
const routes: Array<RouteRecordRaw> = [
{
path: '/',
redirect: '/home',
},
{
path: '/home',
name: 'Home',
component: HomePage,
},
{
path: '/detail',
name: 'Detail',
component: DetailPage,
},
];
假设我们从 home
路由开始,我们想要添加一个按钮,将我们带到 detail
路由。我们可以使用以下 HTML 导航到 detail
路由:
¥Say we start on the home
route, and we want to add a button that takes us to the detail
route. We can do this using the following HTML to navigate to the detail
route:
<ion-button router-link="/detail">Go to detail</ion-button>
我们还可以使用路由 API 以编程方式在我们的应用中导航:
¥We can also programmatically navigate in our app by using the router API:
<template>
<ion-page>
<ion-content>
<ion-button @click="() => router.push('/detail')">Go to detail</ion-button>
</ion-content>
</ion-page>
</template>
<script lang="ts">
import { IonButton, IonContent, IonPage } from '@ionic/vue';
import { defineComponent } from 'vue';
import { useRouter } from 'vue-router';
export default defineComponent({
name: 'HomePage',
components: {
IonButton,
IonContent,
IonPage,
},
setup() {
const router = useRouter();
return { router };
},
});
</script>
两个选项都提供相同的导航机制,只是适合不同的用例。
¥Both options provide the same navigation mechanism, just fitting different use cases.
使用 router-link
导航
¥Navigating using router-link
router-link
属性可以在任何 Ionic Vue 组件上设置,当单击该组件时,路由将导航到指定的路由。router-link
属性接受字符串值以及命名路由,就像 Vue Router 中的 router.push
一样。对于附加控制,还可以设置 router-direction
和 router-animation
属性。
¥The router-link
attribute can be set on any Ionic Vue component, and the router will navigate to the route specified when the component is clicked. The router-link
attribute accepts string values as well as named routes, just like router.push
from Vue Router. For additional control, the router-direction
and router-animation
attributes can be set as well.
router-direction
属性接受 "forward"
、"back"
或 "root"
的值,用于控制页面转换的方向。