简体   繁体   English

如何在 JS 文件中使用导入?

[英]How to use import in JS files?

I got a js file from another developer in wich the developer uses import and not require.我从另一个开发人员那里得到了一个 js 文件,其中开发人员使用导入而不是要求。 It looks like看起来像

import { Selector, Role, ClientFunction } from 'testcafe';
import inputStore from '../config/input-store';
import axios from "axios";
import fs from "fs";
import path from "path";

I want now add another module to this js file, but I have no idea how I can use the import The module I want to use is https://github.com/Olegas/dom-compare我现在想在这个 js 文件中添加另一个模块,但我不知道如何使用 import 我想使用的模块是https://github.com/Olegas/dom-compare

Its docs said:它的文档说:

var compare = require('dom-compare').compare,

What is its import syntax?它的导入语法是什么?

require is part of the CommonJS module syntax. requireCommonJS模块语法的一部分。

import and export are part of the standard ES6 module format which is supported in recent versions of Node.js (but only if explicitly enabled) and in module web browsers when initialized from a <script type="module"> importexport标准 ES6 模块格式的一部分,该格式Node.js 的最新版本(但仅当明确启用时)和从<script type="module">初始化时在模块 Web 浏览器中受支持

Note that your example also uses destructuring syntax to read the value of properties of the imported object without having the store the object itself in an intermediary variable.请注意,您的示例还使用解构语法来读取导入对象的属性值,而无需将对象本身存储在中间变量中。

The equivalent of相当于

var compare = require('dom-compare').compare,

… in ES6 with ES6 modules and destructuring would be: ...在带有 ES6 模块和解构的 ES6 中:

import { compare } from "dom-compare";

tldr:域名:

import {compare} from 'dom-compare';

Explanation:解释:

In these import statements, the word directly to the right of the import keyword is the variable name you can use to access the imported module.在这些 import 语句中,import 关键字右侧的单词是您可以用来访问导入模块的变量名称。 So for example, in this instance, the most direct translation from var compare = require('dom-compare').compare to an import statement would be:因此,例如,在这种情况下,从var compare = require('dom-compare').compare到 import 语句的最直接转换是:

import dom-compare from 'dom-compare';
var compare = dom-compare.compare;

Then you have access to an object named dom-compare in a variable of the same name and access to a property of the dom-compare object in a variable of its same name.然后,您可以在同名变量中访问名为dom-compare的对象,并在同名变量中访问dom-compare对象的属性。 You could call either of these variables something else if you'd like:如果您愿意,您可以将这些变量中的任何一个称为其他名称:

import myComparisonModule from 'dom-compare';
var theComparingProperty = myComparisonModule.compare;

But in order to grab a particular property off of an object, you can also use the shorthand from es6 that does the same thing, but sets the variable name to whatever the property name was:但是为了从对象中获取特定属性,您也可以使用 es6 中的速记来做同样的事情,但将变量名称设置为属性名称:

import dom-compare from 'dom-compare';
var {compare} = dom-compare;

In this syntax, you still have access to two variables ( dom-compare and compare ).在这种语法中,您仍然可以访问两个变量( dom-comparecompare )。 The only difference is that you could've still named the dom-compare variable anything you liked, but the name of the variable that you're setting to the property you got off the dom-compare object is set to whatever it was originally called when it was a property of the object (in this case compare ).唯一的区别是您仍然可以将dom-compare变量命名为您喜欢的任何名称,但是您设置为从dom-compare对象中获得的属性的变量的名称被设置为它最初的名称当它是对象的属性时(在本例中为compare )。

But you can also do this on just one line if you don't need anything else from the dom-compare module except for the compare property, and that's how we get:但是,如果除了compare属性之外不需要dom-compare模块中的任何其他内容,您也可以仅在一行中执行此操作,这就是我们获得的方式:

import {compare} from 'dom-compare';

In this case we don't have a variable to access the dom-compare module, just a variable to access the compare property from the dom-compare module.在这种情况下,我们没有访问dom-compare模块的变量,只有一个访问来自dom-compare模块的compare属性的变量。 And it's named after itself.它以自己的名字命名。

You can also grab multiple properties off of an object in one line.您还可以在一行中从一个对象中获取多个属性。 For example, in the statement import { Selector, Role, ClientFunction } from 'testcafe';例如,在语句import { Selector, Role, ClientFunction } from 'testcafe'; that you referenced, the other developer was pulling multiple properties out of the testcafe object to use as variables of the same names, a shorthand for:您引用的另一个开发人员正在从testcafe对象中提取多个属性以用作同名变量,这是以下内容的简写:

import testcafeModule from 'testcafe';
var Selector = testcafeModule.Selector;
var Role = testcafeModule.Role;
var ClientFunction = testcafeModule.ClientFunction;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM