【问题标题】:Problem trying to render React Component in Laravel Project尝试在 Laravel 项目中渲染 React 组件时出现问题
【发布时间】:2021-05-09 09:21:44
【问题描述】:

我在尝试在我的“登录”视图中呈现我的 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'));
}

我的网页.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');

【问题讨论】:

    标签: php reactjs laravel render


    【解决方案1】:

    我实际上最终解决了这个问题。我将我的 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'));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-16
      • 1970-01-01
      • 2022-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多