【问题标题】:How do i import my file to app,js using javascript?如何使用 javascript 将我的文件导入到 app、js?
【发布时间】:2020-07-20 10:16:12
【问题描述】:

我一直在尝试将 javascript 文件导入到 create-react-app 包中的 app.js。我使用此代码导入我的文件:

注意:我的文件位于名为 components 的文件夹中,您可以在其中找到文件夹 Navigation 及其在该文件夹中。

import script from './components/Navigation/script;

我使用以下方法导出了文件:

export default App;

但在我的终端中我收到一条错误消息

Module not found : cant resolve'./components/Navigation/script'

谁能告诉我如何解决这个错误。 提前谢谢你:-)

【问题讨论】:

  • 您确定导出文件正确吗?像这样----导出默认脚本;
  • app.jscomponents 必须在同一个文件夹中。你确定它们在同一个目录中吗?也许试试This question。这可能会有所帮助。

标签: javascript reactjs import


【解决方案1】:

你的语法错误。

import script from './components/Navigation/script;

实际上是从 script.js 导入默认导出

export default function() {}

您还可以像这样导入命名导出:

import {something} from "./script.js"

哪些进口

const something = 42;
export something;

我猜你可以用

导入js(和css)文件
import "./scripts.js" //or import "./style.css"

但您通常希望导出单个函数、值等。

您也可以从这样的模块中导入所有内容

import * as React from "react";

但 create-react-app 配置允许您将其用作

import React from "react"; //Internally equals to import * as React from "react";

【讨论】:

    猜你喜欢
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 2021-10-03
    • 2020-04-15
    • 2021-06-02
    • 1970-01-01
    • 2022-12-09
    相关资源
    最近更新 更多