当我运行npm start命令时,我收到以下错误:

Failed to compile. 

./src/layouts/BookCheckoutPage/BookCheckoutPage.tsx 131:10

Module parse failed: Unexpected token (131:10)

You may need an appropriate loader to handle this file type.

|       columnNumber: 21
|     }
>   }, book?.img ? /*#__PURE__*/React.createElement("img", {
|     src: book?.img,
|     width: "226",

这是我的BookCheckoutPage.tsx文件:

import React from "react";
import { useEffect, useState } from "react";
import BookModel from "../../models/BookModel";
import { SpinnerLoading } from "../Utils/SpinnerLoading";

export const BookCheckoutPage = () => {

    const [book, setBook] = useState<BookModel>();
    const [isLoading, setIsLoading] = useState(true);
    const [httpError, setHttpError] = useState(null);

    const bookId = (window.location.pathname).split('/')[2];

    useEffect(() => {
        const fetchBook = async () => {
            const baseUrl: string = `http://localhost:8080/api/books/${bookId}`;

            const response = await fetch(baseUrl);

            if (!response.ok) {
                throw new Error('Something went wrong!');
            }

            const responseJson = await response.json();

            const loadedBook: BookModel = {
                id: responseJson.id,
                title: responseJson.title,
                author: responseJson.author,
                description: responseJson.description,
                copies: responseJson.copies,
                copiesAvailable: responseJson.copiesAvailable,
                category: responseJson.category,
                img: responseJson.img,
            };

            setBook(loadedBook);
            setIsLoading(false);
        };
        fetchBook().catch((error: any) => {
            setIsLoading(false);
            setHttpError(error.message);
        })
    }, []);


    if (isLoading) {
        return (
            <SpinnerLoading />
        )
    }

    if (httpError) {
        return (
            <div className='container m-5'>
                <p>{httpError}</p>
            </div>
        )
    }

    return (
        <div>
            <div className='container d-none d-lg-block'>
                <div className='row mt-5'>
                    <div className='col-sm-2 col-md-2'>
                        {book?.img ?
                            <img src={book?.img} width='226' height='349' alt='Book' />
                            :
                            <img src={require('./../../Images/BooksImages/book-luv2code-1000.png')} width='226'
                                height='349' alt='Book' />
                        }
                    </div>
                    <div className='col-4 col-md-4 container'>
                        <div className='ml-2'>
                            <h2>{book?.title}</h2>
                            <h5 className='text-primary'>{book?.author}</h5>
                            <p className='lead'>{book?.description}</p>
                        </div>
                    </div>
                </div>
                <hr />
            </div>
            <div className='container d-lg-none mt-5'>
                <div className='d-flex justify-content-center align-items-center'>
                    {book?.img ?
                        <img src={book?.img} width='226' height='349' alt='Book' />
                        :
                        <img src={require('./../../Images/BooksImages/book-luv2code-1000.png')} width='226'
                            height='349' alt='Book' />
                    }
                </div>
                <div className='mt-4'>
                    <div className='ml-2'>
                        <h2>{book?.title}</h2>
                        <h5 className='text-primary'>{book?.author}</h5>
                        <p className='lead'>{book?.description}</p>
                    </div>
                </div>
                <hr />
            </div>
        </div>
    );
}

我搜索了这个问题,很多帖子都显示这是打字问题、tsconfig问题或requre()方法问题.我requre()%确定这不是因为我已经在我的项目中使用了类似的东西,没有任何错误.例如,这本"返乡书".

import React from 'react'
import BookModel from '../../models/BookModel'

export const ReturnBook: React.FC<{book: BookModel}> = (props) => {
  return (
    <div className="col-xs-6 col-sm-6 col-md-4 col-lg-3 mb-3">
      <div className="text-center">
        {props.book.img ? 
           <img
           src={props.book.img}
           width="151"
           height="233"
           alt="book"
          />
          :
          <img
          src={require('../../Images/BooksImages/book-luv2code-1000.png')}
          width="151"
          height="233"
          alt="book"
          />
        }
        <h6 className="mt-2">{props.book.title}</h6>
        <p>{props.book.author}</p>
        <a className="btn main-color text-white" href="#">
          Reserve
        </a>
      </div>
    </div>
  )
}

推荐答案

看起来我好像是managed to compile it岁.我需要判断它是否真的有效:).如果有人卡住了,代码是这样的:

import React from "react";
import { useEffect, useState } from "react";
import BookModel from "../../models/BookModel";
import { SpinnerLoading } from "../Utils/SpinnerLoading";

export const BookCheckoutPage = () => {

    const [book, setBook] = useState<BookModel>();
    const [isLoading, setIsLoading] = useState(true);
    const [httpError, setHttpError] = useState(null);

    const bookId = (window.location.pathname).split('/')[2];

    useEffect(() => {
        const fetchBook = async () => {
            const baseUrl: string = `http://localhost:8080/api/books/${bookId}`;

            const response = await fetch(baseUrl);

            if (!response.ok) {
                throw new Error('Something went wrong!');
            }

            const responseJson = await response.json();

            const loadedBook: BookModel = {
                id: responseJson.id,
                title: responseJson.title,
                author: responseJson.author,
                description: responseJson.description,
                copies: responseJson.copies,
                copiesAvailable: responseJson.copiesAvailable,
                category: responseJson.category,
                img: responseJson.img,
            };

            setBook(loadedBook);
            setIsLoading(false);
        };
        fetchBook().catch((error: any) => {
            setIsLoading(false);
            setHttpError(error.message);
        })
    }, []);


    if (isLoading) {
        return (
            <SpinnerLoading />
        )
    }

    if (httpError) {
        return (
            <div className='container m-5'>
                <p>{httpError}</p>
            </div>
        )
    }

    return (
        <div>
            <div className='container d-none d-lg-block'>
                <div className='row mt-5'>
                    <div className='col-sm-2 col-md-2'>
                        {typeof book !== 'undefined' && book.img ?
                            <img src={book.img} width='226' height='349' alt='Book' />
                            :
                            <img src={require('./../../Images/BooksImages/book-luv2code-1000.png')} width='226'
                                height='349' alt='Book' />
                        }
                    </div>
                    <div className='col-4 col-md-4 container'>
                        <div className='ml-2'>
                            <h2>{typeof book !== 'undefined' && book.title}</h2>
                            <h5 className='text-primary'>{typeof book !== 'undefined' && book.author}</h5>
                            <p className='lead'>{typeof book !== 'undefined' && book.description}</p>
                        </div>
                    </div>
                </div>
                <hr />
            </div>
            <div className='container d-lg-none mt-5'>
                <div className='d-flex justify-content-center align-items-center'>
                    {typeof book !== 'undefined' && book.img ?
                        <img src={book.img} width='226' height='349' alt='Book' />
                        :
                        <img src={require('./../../Images/BooksImages/book-luv2code-1000.png')} width='226'
                            height='349' alt='Book' />
                    }
                </div>
                <div className='mt-4'>
                    <div className='ml-2'>
                        <h2>{typeof book !== 'undefined' && book.title}</h2>
                        <h5 className='text-primary'>{typeof book !== 'undefined' && book.author}</h5>
                        <p className='lead'>{typeof book !== 'undefined' && book.description}</p>
                    </div>
                </div>
                <hr />
            </div>
        </div>
    );
}

Reactjs相关问答推荐

替换谷歌 map api默认信息窗口与自定义

通过单击具有此值的按钮将值添加到数组对象键

有没有办法在Shopify中显示自定义计算的交货日期?

Reaction中的数据加载不一致

左边框不在图表中显示

这两个子元素在Reaction Native中使用的有什么不同?

为什么我得不到我想要的数据?

Antd V5组件自定义主题

更改点几何体的坐标会将其隐藏

Github 操作失败:发布 NPM 包

如何获取用户在 GooglePlacesAutocomplete 中输入的文本?

获取类别和页面的参数

如何解决使用react-hook-form和yup动态创建的输入不集中的问题

从ReactDataGridtry 将数据传递给父组件

React Router v6 路径中的符号

React - 将一个充满空值的无限大小的数组存储在 ref 中是否可以,或者我应该重置数组吗?

在按钮单击中将动态参数传递给 React Query

如何使用 redux 和 react-plotly.js

如何保留我的下一个授权用户会话?所以我可以使用提供的 ID 在其他路由中获取数据

如何从 React Redux store 中删除特定项目?