reactjs - 除非'--jsx'-React 否则不能使用 JSX
问题描述
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react" <=error
},
"include": [
"./src/**/*.ts"
]
}
登录.tsx
interface IUserLoginInformation {
username: string;
password: string;
}
const Login = () => {
const {
register,
handleSubmit,
watch,
formState: { errors }
} = useForm<IUserLoginInformation>();
const history = useHistory();
const [users, setUsers] = React.useState([]);
const onSubmit: SubmitHandler<IUserLoginInformation> = data =>
fetchLoginDeatils();
return (
<form onSubmit={handleSubmit(onSubmit)}>
<input defaultValue="test" {...register("username")} />{" "}
{/* register your input into the hook by invoking the "register" function */}
{/* include validation with required or other standard HTML validation rules */}
<input {...register("password", { required: true })} />
{/* errors will return when field validation fails */}
{errors.password && <span>This field is required</span>}
<input type="submit" />
</form>
);
};
export default (Login);
在上面的代码中,我收到以下错误。我尝试了互联网上所有可能的选项。但没有任何改变。在登录代码中,我同时使用 javascript 和类型脚本。我也将“jsx”:“react-jsx”更改为“jsx”:“ react” 。我在 tsconfig 出现错误的地方做了标记。 js
Cannot use JSX unless the '--jsx' flag is provided.
解决方案
以下是错误的,因为它排除了.tsx
像 Login.tsx
"include": [
"./src/**/*.ts"
]
使固定
将其更改为:
"include": [
"src"
]
其他提示
- 更改后尝试重新启动 IDE + 终端以获取 tsconfig.json。
- 确保没有其他 tsconfig.json 文件。
推荐阅读
- php - 双 foreach 与 html 集体和采摘
- javascript - 在 Node.js 中嵌套承诺的情况下承诺解决/拒绝
- javascript - 随机数列表缺少数字 1
- sql-server - 如何在 SSRS 报告中动态调整图像的高度和宽度?
- python - 如何将列表分成大小相等的块,如果列表是奇数,我想将块添加到前一个块
- regex - RegEx 用于匹配关键字后的一到三个单词
- user-interface - 有没有办法在 Lua 中创建 GUI?
- android - 未找到 Android SDK,即使设置了 ANDROID_HOME 环境变量,在 cmd 中键入 'android' 也能获得完美的结果
- amazon-web-services - 意外的延迟问题 AWS-API Gateway
- docker - 适用于 Windows Server 2016 的 docker dotnet 2.1 和 2.2 映像