2024-05-27 渥太华微生活

使用 Inertia.js、React.js 和 Vite.js 设置 Laravel 应用:
1、创建应用程序
composer create-project laravel/laravel laravel-react-inertia
用下面命令运行本地服务器,检查网页看看 laravel 是否安装成功。
php artisan serve
2、服务器端设置 (Server-side setup)
1) 安装 Inertia.js 编译器依赖项:
composer require inertiajs/inertia-laravel
2) 将文件 resources/views/welcome.blade.php 重命名为 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>Laravel</title>
@viteReactRefresh
@vite(['resources/css/app.css', 'resources/js/app.jsx'])
@inertiaHead
</head>
<body>
@inertia
</body>
</html>3) 生成 Inertia 中间件
php artisan inertia:middleware
4) 打开 bootstrap/app.php 文件,添加该中间件:
<?php
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use App\Http\Middleware\HandleInertiaRequests;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
$middleware->append(HandleInertiaRequests::class);
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create();3、客户端设置 (Client-side setup)
1) 安装所有依赖项:
npm install @inertiajs/inertia-react @inertiajs/react @vitejs/plugin-react react react-dom
2) 更新 vite.config.js 文件:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [
laravel({
input: ['resources/css/app.css', 'resources/js/app.jsx'],
refresh: true,
}),
react(),
],
});3) 将 resources/js/app.js 重命名为 app.jsx,并将以下代码粘贴到其中:
import React from 'react'
import {createRoot} from 'react-dom/client'
import {createInertiaApp } from '@inertiajs/inertia-react'
import {resolvePageComponent} from 'laravel-vite-plugin/inertia-helpers'
createInertiaApp({
resolve: (name) => resolvePageComponent(`./Pages/${name}.jsx`, import.meta.glob('./Pages/**/*.jsx')),
setup({el, App, props}) {
createRoot(el).render(<App {...props} />)
},
});4) 在 js 文件夹中创建一个“Pages”文件夹,并在该文件夹中创建一个名为“Test.jsx”的测试组件:
export default function Test() {
return (
<h1>This is test component</h1>
)
}4、渲染组件
打开 routes/web.php,尝试在主页上渲染 Test.jsx:
<?php
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;
Route::get('/', function () {
return Inertia::render('Test');
});5、运行测试
在一个终端运行 VITE server
npm run dev
在另一个终端运行 Laravel server
php artisan serve
在浏览器中检查 http://127.0.0.1:8000 是否运行成功。
这些行李箱外观时尚,结实耐用,抗摔耐冻还超顺滑!
这些行李箱正在打折,趁着优惠赶快购买,放假就能美美出行啦!让你的旅行更快意舒适!
人体工学办公椅通过可调节的扶手、头枕、腰托等部件,能根据不同用户身形提供量身定制的支持,帮助保持脊柱自然曲线,减轻长时间久坐带来的身体负担。
减轻久坐疲劳,快乐享受办公!
这些办公椅都在打折呢,需要的朋友赶紧冲!
想喝杯咖啡热茶,冲杯热巧克力,都少不了热水壶。
电热水壶烧水速度快,全天保持水温,喝热水好方便!
还可以在沸腾后自动关闭,防干烧,防溢出,自动断电!
Amazon官网这些热水壶正在打折,需要的朋友赶紧冲!
行车记录仪高清画质,超级夜视,停车监控,减少视觉死角,记录关键证据,省去不少麻烦!
这些行车记录仪正在打折,需要的朋友赶紧冲!
做大餐吃大餐,怎么能少的了空气炸锅呢!
有了它,快手硬菜不要等,畅享健康美味大餐!
这些空气炸锅正在打折,趁着优惠快搞起来!