您好,登录后才能下订单哦!
# Laravel怎么安装Inertia Vue3的版本
## 前言
Inertia.js是一个创新的前端框架,它允许开发者使用Vue、React或Svelte等现代前端框架构建单页面应用(SPA),同时保持传统的服务端渲染(SSR)工作流程。本文将详细介绍如何在Laravel项目中安装和配置Inertia.js与Vue3的组合。
## 环境准备
在开始之前,请确保你的开发环境满足以下要求:
- PHP >= 8.0
- Laravel >= 9.x
- Node.js >= 16.x
- Composer 最新版本
- NPM/Yarn 最新版本
## 第一步:创建新的Laravel项目
如果你还没有Laravel项目,可以通过以下命令创建一个:
```bash
composer create-project laravel/laravel inertia-vue3-demo
cd inertia-vue3-demo
Inertia需要服务端和客户端适配器。首先安装Laravel的服务端适配器:
composer require inertiajs/inertia-laravel
Inertia需要一个根模板文件来加载你的JavaScript应用。创建resources/views/app.blade.php
:
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title inertia>{{ config('app.name', 'Laravel') }}</title>
<!-- Scripts -->
@routes
@vite(['resources/js/app.js', "resources/js/Pages/{$page['component']}.vue"])
@inertiaHead
</head>
<body class="font-sans antialiased">
@inertia
</body>
</html>
生成Inertia中间件:
php artisan inertia:middleware
然后在app/Http/Kernel.php
中注册这个中间件:
protected $middlewareGroups = [
'web' => [
// 其他中间件...
\App\Http\Middleware\HandleInertiaRequests::class,
],
];
现在安装Vue3和Inertia客户端适配器:
npm install @inertiajs/vue3 vue@next @vitejs/plugin-vue
更新vite.config.js
:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '@vitejs/plugin-vue';
export default defineConfig({
plugins: [
laravel({
input: 'resources/js/app.js',
refresh: true,
}),
vue({
template: {
transformAssetUrls: {
base: null,
includeAbsolute: false,
},
},
}),
],
});
修改resources/js/app.js
:
import { createApp, h } from 'vue'
import { createInertiaApp } from '@inertiajs/vue3'
import { resolvePageComponent } from 'laravel-vite-plugin/inertia-helpers'
createInertiaApp({
resolve: (name) => resolvePageComponent(`./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue')),
setup({ el, App, props, plugin }) {
createApp({ render: () => h(App, props) })
.use(plugin)
.mount(el)
},
})
创建resources/js/Pages/Welcome.vue
:
<script setup>
import { Head, Link } from '@inertiajs/vue3'
</script>
<template>
<Head title="Welcome" />
<div class="min-h-screen bg-gray-100">
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 text-gray-900">
<h1 class="text-2xl font-bold mb-4">Welcome to Inertia + Vue3!</h1>
<Link href="/about" class="text-blue-500 hover:underline">About Page</Link>
</div>
</div>
</div>
</div>
</div>
</template>
修改routes/web.php
:
<?php
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;
Route::get('/', function () {
return Inertia::render('Welcome');
});
Route::get('/about', function () {
return Inertia::render('About');
});
创建resources/js/Pages/About.vue
:
<script setup>
import { Head, Link } from '@inertiajs/vue3'
</script>
<template>
<Head title="About" />
<div class="min-h-screen bg-gray-100">
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 text-gray-900">
<h1 class="text-2xl font-bold mb-4">About Page</h1>
<Link href="/" class="text-blue-500 hover:underline">Back to Home</Link>
</div>
</div>
</div>
</div>
</div>
</template>
在app/Http/Middleware/HandleInertiaRequests.php
中,你可以添加共享数据:
public function share(Request $request)
{
return array_merge(parent::share($request), [
'auth' => [
'user' => $request->user(),
],
'flash' => [
'message' => fn () => $request->session()->get('message')
],
]);
}
现在可以启动开发服务器了:
npm run dev
php artisan serve
访问http://localhost:8000
应该能看到你的Inertia + Vue3应用了!
确保你的Vite配置正确,特别是resolvePageComponent
函数的使用。检查文件路径是否正确。
如果你使用Tailwind CSS或其他CSS框架,确保在app.js
中正确导入:
import '../css/app.css'
检查Vite配置中的refresh
选项是否启用,并确保端口没有被占用。
安装Pinia:
npm install pinia
然后在app.js
中设置:
import { createPinia } from 'pinia'
const pinia = createPinia()
createApp({ render: () => h(App, props) })
.use(plugin)
.use(pinia)
.mount(el)
安装@headlessui/vue
和@heroicons/vue
:
npm install @headlessui/vue @heroicons/vue
然后在布局组件中添加过渡:
<template>
<Transition
enter-active-class="transition duration-300"
enter-from-class="opacity-0"
enter-to-class="opacity-100"
leave-active-class="transition duration-300"
leave-from-class="opacity-100"
leave-to-class="opacity-0"
>
<component :is="Component" />
</Transition>
</template>
npm run build
composer install --optimize-autoloader --no-dev
php artisan config:cache
php artisan route:cache
通过以上步骤,你已经成功在Laravel项目中集成了Inertia.js和Vue3。这种组合提供了现代SPA的开发体验,同时保持了Laravel强大的后端功能。Inertia的简单性和Vue3的组合式API使得开发复杂的单页应用变得更加高效。
”`
这篇文章详细介绍了从零开始安装和配置Inertia.js与Vue3的完整过程,涵盖了从基础设置到高级配置的各个方面,并提供了常见问题的解决方案。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。