在React项目中使用JS和TS

J

我按照以下指示创建了一个新的react项目https://docs.microsoft.com/en-us/office/dev/add-ins/quickstarts/excel-quickstart-react

它创建一个打字稿react项目。

我将现有的React JS文件复制到src目录中,并将ts.config更改为

"allowJs": true,

运行npm start会出错

    ERROR in ./components/folder/ContactComponent.js 176:13
Module parse failed: Unexpected token (176:13)
You may need an appropriate loader to handle this file type.
|       const { cookies } = this.props;
|       cookies.set('cookieUserToken', "", { path: '/'})
>       return <Redirect to="/login" />;
|     }

|
 @ ./components/App.tsx 64:25-65
 @ ./index.tsx
 @ multi ../node_modules/webpack-dev-server/client?https://localhost:3000 ../node_modules/webpack/hot/dev-server.js react-hot-loader/patch ./index.tsx

ERROR in ./components/Login/LoginComponent.js 6:4
Module parse failed: Unexpected token (6:4)
You may need an appropriate loader to handle this file type.
| function LoginComponent() {
|   return (
>     <div id="content-main" className="login">
|       <div className="padding">
|         <div className="card card-container">
 @ ./components/App.tsx 63:23-56
 @ ./index.tsx
 @ multi ../node_modules/webpack-dev-server/client?https://localhost:3000 ../node_modules/webpack/hot/dev-server.js react-hot-loader/patch ./index.tsx
Child html-webpack-plugin for "function-file\function-file.html":

ContactComponent使用react-router-dom

render() {
    if (this.state.cookieUrl === "" || this.state.cookieToken === "") {
      const { cookies } = this.props;
      cookies.set('cookieToken', "", { path: '/'})
      return <Redirect to="/login" />;
    }

我的ts.config如下

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "jsx": "react",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "outDir": "dist",
    "allowUnusedLabels": false,
    "noImplicitReturns": true,
    "noUnusedParameters": true,
    "noUnusedLocals": true,
    "allowJs": true,
    "lib": [
      "es7",
      "dom"
    ],
    "pretty": true,
    "typeRoots": [
      "node_modules/@types"
    ]
  },
  "exclude": [
    "node_modules"
  ],
  "compileOnSave": false,
  "buildOnSave": false
}

我的webpack.config.js是

const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require("webpack");

module.exports = {
    devtool: 'source-map',
    entry: {
        app: './src/index.ts',
        'function-file': './function-file/function-file.ts'
    },
    resolve: {
        extensions: ['.ts', '.tsx', '.html', '.js']
    },
    module: {
        rules: [
            {
                test: /\.(tsx|ts|js)$/,
                exclude: /node_modules/,
                use: 'ts-loader'
            },
            {
                test: /\.html$/,
                exclude: /node_modules/,
                use: 'html-loader'
            },
            {
                test: /\.(png|jpg|jpeg|gif)$/,
                use: 'file-loader'
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './index.html',
            chunks: ['app']
        }),
        new HtmlWebpackPlugin({
            template: './function-file/function-file.html',
            filename: 'function-file/function-file.html',
            chunks: ['function-file']
        }),
        new webpack.ProvidePlugin({
            Promise: ["es6-promise", "Promise"]
        })
    ]
};

必须有一种方法可以使JS和TSX正常播放并进行编译,我尝试了一些选择,例如使用已安装和使用的awesome-typescript-loader,但遇到相同的错误。

有没有人尝试过将JS组件与React组件混合在一起,并使这两个组件一起工作?

我将JS文件转换为tsx,一切正常。我说服自己,这是更简单的方法。

谢谢。

用户名

目前,您的webpack配置已设置为使用加载.tsx文件ts-loader,但没有提及.js文件!

这与您收到的错误消息对齐,该错误消息来自webpack,而不是TypeScript:

您可能需要适当的加载程序来处理此文件类型。

您可以通过将修改test: /\.tsx?$/,来更改它test: /\.(tsx|ts|js)$/,

为了测试这一点,我创建了Foo.js:

export var foo = "foo Welcome foo";

还有Foo.d.ts(别忘了,如果您想将JS与TS一起使用,则需要输入内容):

export var foo: string;

然后在index.tsx中,我导入了foo:

import { foo } from './components/Foo';

并使用它:

<AppContainer>
    <>
        <h1>{foo}</h1>
        <Component title={title} isOfficeInitialized={isOfficeInitialized} />
    </>
</AppContainer>,

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在REACT项目中使用Scrollmagic和GSAP

在 React 项目中使用 Material ui 和 Tailwind css

在我的角度项目中使用@ types / cleave.js时,如何忽略TS1192?

在React项目中使用Redux

使用PHPStorm在TypeScript项目中包含* .d.ts文件(和相应的JS)

在混合ES / TS项目中使用Vue组件

如何在React.js项目中使用rainyday.js?

Css 文件未在 React 项目中使用 Express JS 后端加载

如何在Node.js和TypeScript项目中使用URLSearchParams

如何在同一Vue.js项目中使用Realtime Database和Firestore

如何添加 p5.clickable 和 p5.gui 等 p5.js 社区库在 React 项目中使用?

在单独的JS项目中使用Phoenix的`Socket` JS模块

如何在React项目中使用状态

如何在我的 React 项目中使用外部 API?

是否可以在React项目中使用dotenv?

是否可以“部分”在Django项目中使用React?

在React Native项目中使用npm模块

如何在我的React / webpack项目中使用sass?

我可以在Django项目中使用React Router吗?

如何在 Laravel 项目中使用 React?

无法在Android Gradle项目中使用Picasso和Jsoup

在我的 android 项目中使用 firebase 和 AWS

在Swift和ObjectiveC混合项目中使用砌体

在 angular 6 cli 项目中使用 Javascript 和 Jquery

在MVC和Umbraco项目中使用非Umbraco数据?

React-Redux 和 React Context API 可以在同一个项目中使用吗?

2021 年如何在 VS Code 中的 Razzle、React 和 TypeScript 项目中使用 ESLint 和 Prettier?

在Web项目中使用Android项目中的map.osm和osmarender.xml

如何在项目中使用 index.js?