reactjs - 反应解决错误:未捕获的类型错误:无法解析模块说明符“反应”
问题描述
我对 typescript 很陌生,最近尝试使用 ts 构建一些反应页面,但是在试图让它全部工作时遇到了很多问题。
我目前正在尝试将反应(使用打字稿)集成到现有的 asp.net 核心应用程序中,并使用 npm 导入模块。但是尝试加载页面时收到未捕获的类型错误:
未捕获的类型错误:无法解析模块说明符“react”。相对引用必须以“/”、“./”或“../”开头。
我的猜测是“node_modules”库在运行时无法访问,需要以某种方式位于 wwwroot 文件夹中(但我可能错了)。目前 node_modules 文件夹与 wwwroot 文件夹处于同一目录级别。
我的 tsconfig.json:
{
"compilerOptions": {
"target": "ES6",
"lib": ["ES6", "dom"],
"moduleResolution": "Node",
"outDir": "../wwwroot/js/",
"jsx": "react"
}
}
npm 包:
├── @types/bootstrap@5.0.17
├── @types/jquery@3.5.6
├── @types/node@16.4.3
├── @types/react-dom@17.0.9
├── @types/react@17.0.15
├── commonjs@0.0.1
├── react-dom@17.0.2
├── react@17.0.2
└── typescript@4.3.5
剃刀页面:
@page
@using System.Data
@using Microsoft.AspNetCore.Html
@model TestSite.Form
@{
ViewData["Title"] = " Form";
}
<div>
<div id='FormContent'></div>
<script type="module" src="/js/Builder/Form.js"></script>
</div>
表格.tsx:
import * as React from 'react';
import { render } from 'react-dom';
class Form extends React.Component
{
constructor()
{
super();
}
render()
{
return (
<div>
<b>Test</b>
</div>
)
}
}
render(
<Form />,
document.getElementById('FormContent')
);
关于这个有什么想法吗?
提前谢谢了!:)
解决方案
推荐阅读
- android - 在我的应用程序中,微调项目选择是强制性的。如果用户没有选择任何项目,我如何向用户显示错误?
- django - 如何在 Django F 表达式中将日期和时间字段合并到 DateTime 字段中
- python - 如何将 N 个多列表合并为一个大的多列表?
- arrays - 快速调用解析函数失败
- r - 如何在 R 中绘制稀疏矩阵?
- r - How do I subset a list with mixed data type and data structure?
- elasticsearch - Elasticsearch term query to number token
- angular - 网站没有在服务器上加载,同时从浏览器以角度 2 刷新它
- python - 如何读取缺少值和'delim_whitespace = True'的csv文件?
- javascript - Change state/prop in video react-native-video component