javascript es6 import“只需要一个参数”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48140375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:49:51  来源:igfitidea点击:

javascript es6 import "expects exactly one argument"

javascriptecmascript-6

提问by PopKernel

I'm trying to use es6 modules but I'm hitting an error:

我正在尝试使用 es6 模块,但遇到错误:

SyntaxError: Unexpected identifier '{'. import call expects exactly one argument.

语法错误:意外的标识符“{”。导入调用只需要一个参数。

This is in Safari 11 on macOS 10.13, by the way.

顺便说一下,这是在 macOS 10.13 上的 Safari 11 中。

Here's my module:

这是我的模块:

export class GameObject {
    //code
}

export class GameLoop {
    //code
}

relevant html:

相关的html:

<body>
    <script type="module" src="gameFoundation.js"></script>
    <script src="gameTest.js"></script>
</body>

and the script that tries to use the module, which gives the aforementioned error on line 1:

以及尝试使用该模块的脚本,它在第 1 行给出了上述错误:

import { GameObject, GameLoop } from "./gameFoundation.js"

class Rect extends GameObject {
    //code
}

I'm new to JavaScript, so I imagine I'm getting something basic wildly wrong. Any help would be much appreciated.

我是 JavaScript 的新手,所以我想我遇到了一些基本的错误。任何帮助将非常感激。

采纳答案by T.J. Crowder

Your exports are named, but you're using defaultimport syntax. You need to wrap the names of what you're importing in {...}:

您的导出名为,但您使用的是默认导入语法。您需要将要导入的内容的名称包装起来{...}

import { GameObject } from "./gameFoundation.js";
import { GameLoop } from "./gameFoundation.js";

You can also do both with one import declaration if you like:

如果您愿意,您也可以使用一个导入声明来执行这两项操作:

import { GameObject, GameLoop } from "./gameFoundation.js";

Also note that importis only valid in a module, so you need to change:

另请注意,import在 module 中有效,因此您需要更改:

<script src="gameTest.js"></script>

to

<script type="module" src="gameTest.js"></script>

回答by The Reason

Named exports will work for you.

命名导出对您有用。

Replace

代替

import GameObject from "./gameFoundation.js"
import GameLoop from "./gameFoundation.js"

to

import { GameObject, GameLoop } from "./gameFoundation.js"

Hereis a good article about all ES6 import/exports. Will be useful to read for you.

是一篇关于所有 ES6导入/导出的好文章。对你阅读会有帮助。

回答by Himanshu Soni

You are using ES6 syntax which needs to be transpiled into native plain old javascript syntax to get loaded into the browser.

您正在使用 ES6 语法,需要将其转换为原生的普通旧 javascript 语法才能加载到浏览器中。

Please, refer to the following link: https://scotch.io/tutorials/javascript-transpilers-what-they-are-why-we-need-them

请参考以下链接:https: //scotch.io/tutorials/javascript-transpilers-what-they-are-why-we-need-them

It will guide you about transpiler that may solve your problem.

它将指导您了解可以解决您问题的转译器。

回答by Arnold Gandarillas

In es6 to make imports like this:

在 es6 中进行这样的导入:

import GameObject from "./gameFoundation.js"

your export should look like this:

您的导出应如下所示:

export default class GameObject { }

in your case you need to make your imports like this:

在您的情况下,您需要像这样进行导入:

import { GameObject, GameLoop } from './gameFoundation.js'