简体   繁体   English

尝试在 Laravel 项目中呈现 React 组件时出现问题

[英]Problem trying to render React Component in Laravel Project

I am having issues trying to render my React component within my 'login' view.我在尝试在我的“登录”视图中呈现我的 React 组件时遇到问题。 I am using the Laravel Framework.我正在使用 Laravel 框架。 The page is loaded, however I find no React component in my React Devtools.页面已加载,但我在 React Devtools 中找不到 React 组件。 The Example component provided in the Laravel Project template renders fine. Laravel 项目模板中提供的示例组件渲染良好。 I could not find an error in my syntax, and I was hoping one of you could help me find my error.我在我的语法中找不到错误,我希望你们中的一个人能帮助我找到我的错误。

My login.blade.php我的 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>

My LoginForm.js我的 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'));
}

My web.php我的 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');
});

My app.js我的 app.js

require('./bootstrap');


require('./components/Example');
require('./components/LoginForm');

I actually ended up solving this.我实际上最终解决了这个问题。 I changed my LoginForm.js to look like this.我将我的 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM