[英]Problem trying to render React Component in Laravel Project
我在尝试在我的“登录”视图中呈现我的 React 组件时遇到问题。 我正在使用 Laravel 框架。 页面已加载,但我在 React Devtools 中找不到 React 组件。 Laravel 项目模板中提供的示例组件渲染良好。 我在我的语法中找不到错误,我希望你们中的一个人能帮助我找到我的错误。
我的 login.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 Login</title>
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Nunito:wght@200;600&display=swap" rel="stylesheet">
<link href="{{ URL::asset('public/css/app.css') }}">
</head>
<body>
<div id="login-form">
</div>
<script defer src='./js/app.js'> </script>
</body>
</html>
我的 LoginForm.js
import React from 'react';
import ReactDOM from 'react-dom';
function LoginForm() {
return (
<div class= "container">
Insert FORM here TEST
</div>
);
}
export default LoginForm;
if (document.getElementById('login-form')) {
ReactDOM.render(<LoginForm />, document.getElementById('login-form'));
}
我的 web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/login', function () {
return view('login');
});
我的 app.js
require('./bootstrap');
require('./components/Example');
require('./components/LoginForm');
我实际上最终解决了这个问题。 我将我的 LoginForm.js 更改为如下所示。
import React, {component} from 'react';
import ReactDOM from 'react-dom';
export default class LoginForm extends React.Component{
render(){
return (
<div class= "container">
Insert FORM here TEST
</div>
);
}
}
if (document.getElementById('login-form')) {
ReactDOM.render(<LoginForm />, document.getElementById('login-form'));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.