“InertiaJS – React is not defined” 的中文翻译是: “InertiaJS – React 未定义”

huangapple go评论54阅读模式
英文:

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 reactnpm 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 &#39;@inertiajs/react&#39;
import { createRoot } from &#39;react-dom/client&#39;

createInertiaApp({
  resolve: name =&gt; {
    const pages = import.meta.glob(&#39;./Pages/**/*.jsx&#39;, { eager: true })
    return pages[`./Pages/${name}.jsx`]
  },
  setup({ el, App, props }) {
    createRoot(el).render(&lt;App {...props} /&gt;)
  },
})

I have the following in resources/js/Pages/HelloWorld.jsx:

import React from &#39;react&#39;
import { Head } from &#39;@inertiajs/react&#39;

export default function HelloWorld () {
  return (
    &lt;div&gt;
      &lt;Head title=&quot;Hello World&quot; /&gt;
      &lt;h1&gt;Hello World&lt;/h1&gt;
    &lt;/div&gt;
  )
}

And the following in routes/web.php:

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

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

https://laravel.com/docs/10.x/vite#react

英文:

You need to install@vitejs/plugin-react

https://laravel.com/docs/10.x/vite#react

huangapple
  • 本文由 发表于 2023年5月25日 03:27:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326826.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定