介绍
想从头开始使用Nuxt构建一个移动应用吗?本教程将带您一步一步地创建一个从一开始就配置为移动的新Nuxt 4项目,然后使用__CAPGO_KEEP_0__将其打包为原生iOS和Android应用 Capacitor 8.
通过完成本教程,您将拥有一个可以在模拟器上运行的工作移动应用,可以继续开发并最终发布到App Store和Google Play
所需时间: ~30分钟
您将构建:
- 一个新的 Nuxt 4 项目,带有最新的目录结构
- 静态生成配置文件(适用于移动端)
- Capacitor 8(带有必需的插件)
- 原生 iOS 和 Android 应用
- 实时重新加载开发环境
已有一个 Nuxt 应用?请查看 将您的 Nuxt 应用转换为移动应用 而不是。
前提条件
确保已安装这些:
- Node.js 18+ (请检查
node --version) - Bun 包管理器(
curl -fsSL https://bun.sh/install | bash) - Xcode (仅限macOS,用于iOS开发)
- Android Studio (用于Android开发)
步骤 1:创建一个新的 Nuxt 4 项目
首先创建一个新的 Nuxt 4 项目:
bunx nuxi@latest init my-mobile-app
cd my-mobile-app
bun install
Nuxt 4 目录结构
Nuxt 4 uses a new directory structure with app code in the app/ 目录:
my-mobile-app/
app/
assets/
components/
composables/
layouts/
middleware/
pages/
plugins/
utils/
app.vue
public/
server/
nuxt.config.ts
package.json
This structure provides better separation between app and server code.
步骤 2:配置 Nuxt 静态生成
Capacitor requires static HTML/JS/CSS files. Configure Nuxt for static generation in nuxt.config.ts:
export default defineNuxtConfig({
compatibilityDate: '2025-01-15',
devtools: { enabled: true },
// Enable static generation
ssr: true,
nitro: {
preset: 'static',
},
});
步骤 3:添加移动脚本
更新您的 package.json 以移动开发脚本:
{
"scripts": {
"dev": "nuxt dev",
"build": "nuxt build",
"generate": "nuxt generate",
"preview": "nuxt preview",
"mobile": "bun run generate && bunx cap sync",
"mobile:ios": "bun run mobile && bunx cap open ios",
"mobile:android": "bun run mobile && bunx cap open android"
}
}
测试静态生成:
bun run generate
您应该看到一个 .output/public 目录中您的静态文件。
Step 4: Install Capacitor 8
安装 Capacitor 核心包:
bun add @capacitor/core
bun add -D @capacitor/cli
安装大多数移动应用程序需要的必备插件:
bun add @capacitor/app @capacitor/keyboard @capacitor/splash-screen @capacitor/status-bar @capacitor/preferences
这些插件的作用是:
- @capacitor/app — 前台/后台事件、深度链接
- @capacitor/keyboard — 控制键盘行为
- @capacitor/splash-screen — 原生启动屏幕控制
- @capacitor/status-bar — 样式化设备状态栏
- @capacitor/preferences — 原生键值存储(类似localStorage)
步骤 5:初始化 Capacitor
初始化 Capacitor:
bunx cap init "My Mobile App" com.example.mymobileapp --web-dir .output/public
替换:
"My Mobile App"用你的应用程序的显示名称com.example.mymobileapp用反向域名表示法的应用程序 ID
这会创建 capacitor.config.ts. 更新它:
import type { CapacitorConfig } from '@capacitor/cli';
const config: CapacitorConfig = {
appId: 'com.example.mymobileapp',
appName: 'My Mobile App',
webDir: '.output/public',
plugins: {
SplashScreen: {
launchShowDuration: 2000,
launchAutoHide: true,
androidScaleType: 'CENTER_CROP',
splashFullScreen: true,
splashImmersive: true,
},
Keyboard: {
resize: 'body',
resizeOnFullScreen: true,
},
StatusBar: {
style: 'dark',
},
},
};
export default config;
步骤 6:添加原生平台
安装平台包:
bun add @capacitor/ios @capacitor/android
生成原生项目:
bunx cap add ios
bunx cap add android
这会创建 ios 和包含原生项目的目录。 android 第 7 步:构建和运行
构建您的项目并同步到原生平台:
在 iOS 模拟器中打开:
bun run mobile
或 Android 模拟器:
bun run mobile:ios
在 Xcode (iOS) 中:
bun run mobile:android
从设备下拉菜单中选择一个模拟器
- 点击播放按钮或按
- 在 Android Studio 中:
Cmd + R
等待 Gradle 完成同步
- 从设备下拉菜单中选择一个模拟器
- __CAPGO_KEEP_0__
- 点击运行按钮或按
Shift + F10
步骤 8: 设置实时重载
为了更快地开发,启用实时重载,使设备上更改立即显示。
- 找到您的本地 IP 地址:
# macOS
ipconfig getifaddr en0
# Windows
ipconfig
- 创建开发 Capacitor 配置。更新
capacitor.config.ts:
import type { CapacitorConfig } from '@capacitor/cli';
const devConfig: CapacitorConfig = {
appId: 'com.example.mymobileapp',
appName: 'My Mobile App',
webDir: '.output/public',
server: {
url: 'http://YOUR_IP_ADDRESS:3000',
cleartext: true,
},
plugins: {
// ... same plugin config
},
};
const prodConfig: CapacitorConfig = {
appId: 'com.example.mymobileapp',
appName: 'My Mobile App',
webDir: '.output/public',
plugins: {
// ... same plugin config
},
};
const config = process.env.NODE_ENV === 'development' ? devConfig : prodConfig;
export default config;
- 启动开发服务器并将配置复制到本机:
bun run dev &
NODE_ENV=development bunx cap copy
- 在 Xcode/Android Studio 中重建
现在,您的 Nuxt code 编辑将在设备上实时重载。
步骤 9: 创建第一个移动屏幕
让我们创建一个移动友好的主屏幕。更新 app/app.vue:
<template>
<NuxtPage />
</template>
创建 app/pages/index.vue:
<template>
<main
class="min-h-screen bg-linear-to-b from-green-500 to-green-700 flex flex-col items-center justify-center p-6 text-white"
>
<h1 class="text-4xl font-bold mb-4">My Mobile App</h1>
<p class="text-xl mb-8 text-center opacity-90">
Built with Nuxt 4 + Capacitor 8
</p>
<div v-if="appInfo" class="bg-white/20 rounded-lg p-4 backdrop-blur-sm mb-8">
<p class="text-sm">
{{ appInfo.name }} v{{ appInfo.version }}
</p>
</div>
<div class="space-y-4 w-full max-w-sm">
<button
class="w-full py-4 px-6 bg-white text-green-600 rounded-xl font-semibold text-lg shadow-lg active:scale-95 transition-transform"
@click="handleGetStarted"
>
Get Started
</button>
<button
class="w-full py-4 px-6 bg-white/20 text-white rounded-xl font-semibold text-lg backdrop-blur-sm active:scale-95 transition-transform"
@click="handleShare"
>
Share App
</button>
</div>
</main>
</template>
<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';
import { App } from '@capacitor/app';
const appInfo = ref<{ name: string; version: string } | null>(null);
let backButtonListener: { remove: () => void } | null = null;
onMounted(async () => {
// Get app info
try {
appInfo.value = await App.getInfo();
} catch (e) {
// Web fallback
appInfo.value = { name: 'My Mobile App', version: '1.0.0' };
}
// Handle Android back button
backButtonListener = await App.addListener('backButton', ({ canGoBack }) => {
if (!canGoBack) {
App.exitApp();
} else {
window.history.back();
}
});
});
onUnmounted(() => {
backButtonListener?.remove();
});
function handleGetStarted() {
// Navigate to onboarding or main app
console.log('Get started clicked');
}
async function handleShare() {
// We'll implement this with the Share plugin later
console.log('Share clicked');
}
</script>
步骤 10: 添加 Tailwind CSS
为了让样式生效,请将 Tailwind CSS 添加到您的项目中:
bun add tailwindcss @tailwindcss/vite
更新 nuxt.config.ts:
import tailwindcss from '@tailwindcss/vite';
export default defineNuxtConfig({
compatibilityDate: '2025-01-15',
devtools: { enabled: true },
ssr: true,
nitro: {
preset: 'static',
},
css: ['~/assets/css/main.css'],
vite: {
plugins: [tailwindcss()],
},
});
创建 app/assets/css/main.css:
@import 'tailwindcss';
:root {
--sat: env(safe-area-inset-top);
--sar: env(safe-area-inset-right);
--sab: env(safe-area-inset-bottom);
--sal: env(safe-area-inset-left);
}
body {
padding-top: var(--sat);
padding-right: var(--sar);
padding-bottom: var(--sab);
padding-left: var(--sal);
}
/* Prevent text selection on mobile */
* {
-webkit-user-select: none;
user-select: none;
-webkit-tap-highlight-color: transparent;
}
/* Allow text selection in inputs */
input,
textarea {
-webkit-user-select: auto;
user-select: auto;
}
第 11 步:添加分享插件
让我们实现分享按钮的功能:
bun add @capacitor/share
更新 app/pages/index.vue 以使用分享插件:
<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';
import { App } from '@capacitor/app';
import { Share } from '@capacitor/share';
// ... existing code ...
async function handleShare() {
try {
await Share.share({
title: 'Check out this app!',
text: 'Built with Nuxt 4 and Capacitor 8',
url: 'https://capacitorjs.com',
dialogTitle: 'Share with friends',
});
} catch (e) {
console.log('Share cancelled or failed:', e);
}
}
</script>
同步并重建:
bun run mobile
项目结构
您的项目现在应该像这样:
my-mobile-app/
├── android/ # Android native project
├── ios/ # iOS native project
├── .output/
│ └── public/ # Static build output
├── app/
│ ├── assets/
│ │ └── css/
│ │ └── main.css
│ ├── pages/
│ │ └── index.vue
│ └── app.vue
├── capacitor.config.ts # Capacitor configuration
├── nuxt.config.ts # Nuxt configuration
├── package.json
└── ...
下一步
您现在已经拥有一个工作的 Nuxt 移动应用。接下来要做的事情是:
基本设置
- App 图标: 替换默认图标在
ios/App/App/Assets.xcassets和android/app/src/main/res - 启动屏幕: 在原生项目中自定义或使用
@capacitor/splash-screen配置 - 深度链接: 配置应用程序的 URL 方案
添加更多功能
- 相机:
bun add @capacitor/camera - 地理位置:
bun add @capacitor/geolocation - 推送通知:
bun add @capacitor/push-notifications - 文件系统:
bun add @capacitor/filesystem
UI 增强
考虑添加 Konsta UI 为原生 iOS/Android 组件:
bun add konsta
然后更新您的 CSS 以导入主题:
@import 'tailwindcss';
@import 'konsta/theme.css';
即时更新
设置 Capgo 以推送更新而无需重新提交应用商店:
bunx @capgo/cli init
故障排除
构建失败,提示“找不到模块”
运行 bun install 然后再试一次。
iOS:“找不到签名身份” 打开 Xcode,进入 Signing & Capabilities,选择开发团队。
Android:“SDK”位置未找到
创建 android/local.properties 并 sdk.dir=/path/to/android/sdk
设备上未显示更改
确保你已经运行 bun run mobile 在修改后。对于实时重载,请验证 IP 地址是否正确并且开发服务器正在运行。
.output/public 文件夹为空或不存在
确保您已配置 nitro: { preset: 'static' } 在 nuxt.config.ts 并运行 bun run generate.
资源
准备好将您的应用程序交付?了解如何使用 Capgo 快速交付更新 — 免费注册 今天。