reactjs InertiaJS -未定义React

vyswwuz2  于 2023-05-28  发布在  React
关注(0)|答案(1)|浏览(421)

我按照InertiaJS网站上的服务器端和客户端说明在现有的Laravel项目中设置它。
这是我的app.jsx文件:

import { createInertiaApp } from '@inertiajs/react'
import { createRoot } from 'react-dom/client'

createInertiaApp({
  resolve: name => {
    const pages = import.meta.glob('./Pages/**/*.jsx', { eager: true })
    return pages[`./Pages/${name}.jsx`]
  },
  setup({ el, App, props }) {
    createRoot(el).render(<App {...props} />)
  },
})

我在resources/js/Pages/HelloWorld.jsx中有以下内容:

import React from 'react'
import { Head } from '@inertiajs/react'

export default function HelloWorld () {
  return (
    <div>
      <Head title="Hello World" />
      <h1>Hello World</h1>
    </div>
  )
}

下面是routes/web.php:

Route::get('/hello', function () {
    return \Inertia\Inertia::render('HelloWorld');
});

当我运行服务器并访问/hello时,我得到一个空白页面,在控制台中显示一个错误,说React未定义。
我试过npm install reactnpm install react-dom,但没有成功。我已经尝试清除缓存,删除node_modules并再次运行npm install。它仍然只是说React没有定义。
会是什么问题呢?

toiithl6

toiithl61#

安装@vitejs/plugin-react
https://laravel.com/docs/10.x/vite#react

相关问题