簡體   English   中英

嘗試在 Laravel 項目中呈現 React 組件時出現問題

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM