英文:
InertiaJS - React is not defined
问题
以下是您要翻译的内容:
"我按照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 react
和npm install react-dom
,但没有成功。我尝试过清除缓存、删除node_modules并重新运行npm install,但它仍然显示React未定义。
可能的问题是什么?"
英文:
I followed the server-side and client-side instructions on the InertiaJS website for setting it up in an existing Laravel project.
This is my app.jsx file:
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} />)
},
})
I have the following in 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>
)
}
And the following in routes/web.php:
Route::get('/hello', function () {
return \Inertia\Inertia::render('HelloWorld');
});
When I run the server and visit /hello I get a blank page with an error in the console saying React is not defined.
I have tried npm install react
and npm install react-dom
with no success. I have tried clearing cache, deleting node_modules and running npm install again. It continues to just say that React is not defined.
What could be the issue?
答案1
得分: 0
你需要安装@vitejs/plugin-react
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论