Ionic Vue 快速入门
欢迎!本指南将引导您了解 Ionic Vue 开发的基础知识。您将学习如何设置开发环境、生成一个简单的项目、探索项目结构,并理解 Ionic 组件的工作原理。这对于在构建第一个真实应用之前熟悉 Ionic Vue 来说是完美的。
如果您想了解 Ionic Vue 是什么以及它如何融入 Vue 生态系统的高层次概述,请参见 Ionic Vue 概述。
先决条件
开始之前,请确保您的计算机上已安装 Node.js 和 npm。 您可以运行以下命令来检查:
node -v
npm -v
如果您没有 Node.js 和 npm,请在此处下载 Node.js(其中包含 npm)。
使用 Ionic CLI 创建项目
首先,安装最新的 Ionic CLI:
npm install -g @ionic/cli
然后,运行以下命令来创建并运行一个新项目:
ionic start myApp blank --type vue
cd myApp
ionic serve
运行 ionic serve 后,您的项目将在浏览器中打开。

探索项目结构
您 新应用的目录结构如下:
└── src/
├── App.vue
├── main.ts
├── router
│ └── index.ts
└── views
└── HomePage.vue
以下示例中的所有文件路径都是相对于项目根目录的。
让我们逐一查看这些文件,以了解应用的结构。
查看应用根组件
应用根组件定义在 App.vue 中:
<template>
<ion-app>
<ion-router-outlet />
</ion-app>
</template>
<script setup lang="ts">
import { IonApp, IonRouterOutlet } from '@ionic/vue';
</script>
这设置了应用的根组件,使用了 Ionic 的 ion-app 和 ion-router-outlet 组件。路由出口(router outlet)是您的页面将要显示的地方。
查看路由
路由定义在 router/index.ts 中:
import { createRouter, createWebHistory } from '@ionic/vue-router';
import { RouteRecordRaw } from 'vue-router';
import HomePage from '../views/HomePage.vue';
const routes: Array<RouteRecordRaw> = [
{
path: '/',
redirect: '/home',
},
{
path: '/home',
name: 'Home',
component: HomePage,
},
];
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes,
});
export default router;
当您访问根 URL (/) 时,将加载 HomePage 组件。
查看主页
主页组件定义在 HomePage.vue 中,它导入 Ionic 组件并定义页面模板:
<template>
<ion-page>
<ion-header :translucent="true">
<ion-toolbar>
<ion-title>空白</ion-title>
</ion-toolbar>
</ion-header>
<ion-content :fullscreen="true">
<ion-header collapse="condense">
<ion-toolbar>
<ion-title size="large">空白</ion-title>
</ion-toolbar>
</ion-header>
<div id="container">
<strong>准备好创建应用了吗?</strong>
<p>
从 Ionic
<a target="_blank" rel="noopener noreferrer" href="https://ionicframework.com/docs/components"
>UI 组件</a
>
开始
</p>
</div>
</ion-content>
</ion-page>
</template>
<script setup lang="ts">
import { IonContent, IonHeader, IonPage, IonTitle, IonToolbar } from '@ionic/vue';
</script>
<!-- ...样式... -->
这创建了一个带有头部和可滚动内容区域的页面。第二个头部显示一个可折叠大标题,当位于内容顶部时显示,向下滚动时则缩合为第一个头部中显示的小标题。
添加 Ionic 组件
您可以使用更多 Ionic UI 组件来增强主页。例如,在 ion-content 的末尾添加一个按钮:
<ion-content>
<!-- 现有内容 -->
<ion-button>导航</ion-button>
</ion-content>
然后,在 <script> 标签中导入 IonButton 组件:
<script setup lang="ts">
import { IonButton, IonContent, IonHeader, IonPage, IonTitle, IonToolbar } from '@ionic/vue';
</script>
添加新页面
在 NewPage.vue 创建一个新页面:
<template>
<ion-page>
<ion-header :translucent="true">
<ion-toolbar>
<ion-buttons slot="start">
<ion-back-button default-href="/"></ion-back-button>
</ion-buttons>
<ion-title>新页面</ion-title>
</ion-toolbar>
</ion-header>
<ion-content :fullscreen="true">
<ion-header collapse="condense">
<ion-toolbar>
<ion-title size="large">新页面</ion-title>
</ion-toolbar>
</ion-header>
</ion-content>
</ion-page>
</template>
<script setup lang="ts">
import { IonBackButton, IonButtons, IonContent, IonHeader, IonPage, IonTitle, IonToolbar } from '@ionic/vue';
</script>
这将创建一个在工具栏中带有返回按钮的页面。返回按钮将自动处理导航回上一页,如果没有历史记录则导航到 /。
创建自己的页面时,请始终将 ion-page 作为根组件。这对于页面之间的正确过渡、Ionic 组件依赖的基础 CSS 样式以及应用中一致的布局行为至关重要。
导航到新页面
要导航到新页面,首先在 router/index.ts 的顶部(在 HomePage 导入之后)导入它:
import NewPage from '../views/NewPage.vue';
然后,在 routes 数组中添加其路由:
const routes: Array<RouteRecordRaw> = [
{
path: '/',
redirect: '/home',
},
{
path: '/home',
name: 'Home',
component: HomePage,
},
{
path: '/new',
name: 'New',
component: NewPage,
},
];
完成后,更新 HomePage.vue 中的按钮:
<ion-button router-link="/new">导航</ion-button>
导航也可以使用 Vue Router 以编程方式执行,并且可以懒加载路由以获得更好的性能。更多信息请参见 Vue 导航文档。