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/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>Blank</ion-title>
</ion-toolbar>
</ion-header>
<ion-content :fullscreen="true">
<ion-header collapse="condense">
<ion-toolbar>
<ion-title size="large">Blank</ion-title>
</ion-toolbar>
</ion-header>
<div id="container">
<strong>Ready to create an app?</strong>
<p>
Start with Ionic
<a target="_blank" rel="noopener noreferrer" href="https://ionicframework.com/docs/components"
>UI Components</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 的末尾添加一个 Button:
<ion-content>
<!-- 现有内容 -->
<ion-button>Navigate</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>New</ion-title>
</ion-toolbar>
</ion-header>
<ion-content :fullscreen="true">
<ion-header collapse="condense">
<ion-toolbar>
<ion-title size="large">New</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>