如何在我的 .tsx 文件中导入 .js 文件

j5j*_*ice 8 javascript typescript reactjs

我遇到了 webpack 告诉我的问题:

./app/app.tsx (4,25) 中的错误:错误 TS2307:找不到模块“./sample-data”。

我的进口看起来像这样:

import * as React from 'react';
import * as ReactDom from 'react-dom';
import { InboxPane } from './components/Inbox';
import * as Samples from './sample-data'; 
Run Code Online (Sandbox Code Playgroud)

最后,这是我尝试导入的 sample-data.js 文件:

module.exports = {
  "humans": {
    "John Smith" : {
      "conversations": [
        {
          "who": "bot",
          "text": "Hello, can I take your order?",
          "time": new Date(2016, 4, 5, 15, 10, 0, 0)
        },
        {
          "who": "human",
          "text": "Can I have a small meat-lovers pizza?",
          "time": new Date(2016, 4, 5, 15, 10, 30, 0)
        }, 
        {
          "who": "bot",
          "text": "Where would you like it delivered?",
          "time": new Date(2016, 4, 5, 15, 11, 0, 0)
        },
        {
          "who": "human",
          "text": "123 Sesame Street, Montreal, Canada",
          "time": new Date(2016, 4, 5, 15, 11, 30, 0)
        },
      ],
      "orders": [
        {
          "time": new Date(2016, 4, 5, 15, 11, 45, 0),
          "pizzas": [{
            "toppings": ["Meat-Lovers"],
            "size": "S"
          }],
          "price": 15,
          "address": "321 Sesame Street, Montreal, Canada",
          "status": "Confirmed" // status := Open -> Confirmed -> In The Oven -> Delivered
        }
      ]
    },
    "Alan Foster" : {
      "conversations": [
        {
          "who": "bot",
          "text": "Hello, can I take your order?",
          "time": new Date(2016, 4, 4, 20, 30, 0, 0)
        },
        {
          "who": "human",
          "text": "I would like to order an extra-large cheese pizza",
          "time": new Date(2016, 4, 4, 20, 30, 15, 0)
        }, 
        {
          "who": "bot",
          "text": "Where would you like it delivered?",
          "time": new Date(2016, 4, 4, 20, 30, 30, 0)
        },
        {
          "who": "human",
          "text": "123 Sesame Street, Montreal, Canada",
          "time": new Date(2016, 4, 4, 20, 30, 45, 0)
        },
      ],
      "orders": [
        {
          "time": new Date(2016, 4, 4, 20, 31, 0, 0),
          "pizzas": [{
            "toppings": ["cheese"],
            "size": "XL"
          }],
          "price": 15,
          "address": "123 Sesame Street, Montreal, Canada",
          "status": "Delivered" // status := Open -> Confirmed -> In The Oven -> Delivered
        }
      ]
    }
  }
};
Run Code Online (Sandbox Code Playgroud)

如果我将其更改为 sample-data.ts,我会被告知它不是一个模块。我如何将它加载到我的 .tsx 文件中?

Kon*_*sky 6

如果您想不加修改地导入原始文件,请尝试在您的tsconfig.json文件中设置compilerOptions.allowJs为:true

...
  "compilerOptions": {
    "allowJs": true
...
Run Code Online (Sandbox Code Playgroud)

如果您准备修改文件,您可以使用模块语法将sample-data.js转换为sample-data.ts,如下所示:

export default {
  "humans": {
    "John Smith" : {
      "conversations": [
        {
          "who": "bot",
          "text": "Hello, can I take your order?",
          "time": new Date(2016, 4, 5, 15, 10, 0, 0)
        },
        {
          "who": "human",
        ...
};
Run Code Online (Sandbox Code Playgroud)

然后,您需要通过以下方式导入文件:

import Samples from './sample-data';
Run Code Online (Sandbox Code Playgroud)

您可以在官方文档中阅读有关 TypeScript 模块的更多信息。在许多情况下,TypeScript 中的模块的工作方式与 ES2015 中的相同。