简体   繁体   English

es6中import Task和import {Task}有什么区别

[英]what is the difference between import Task and import { Task } in es6

What is the difference between 之间有什么区别

import { Tasks } from '../api/tasks.js';

and

import Task from './Task.jsx';

when to use {} and when to not use {} ? 什么时候使用{}和什么时候不使用{}

(by the way, this is from meteor tutorial https://www.meteor.com/tutorials/react/update-and-remove ) (顺便说一句,这是从流星教程https://www.meteor.com/tutorials/react/update-and-remove中获得的

You don't have to use the {} when you precise that it's a default export. 精确确定它是默认导出时,不必使用{}。

For example : 例如 :

export default class Test{}

You can do : 你可以做 :

import Test from './test'

In the other hand, if you don't precise "default" keyword, you have to precise {} : 另一方面,如果您不精确使用“默认”关键字,则必须精确使用{}:

export class Test {}

gives

import { Test } from './test'

if you want to grab the all modules you can do 如果您想获取所有模块,则可以执行

import * as test from "Test";

If you exporting only some modules and not all then you have to specify wictch module you want 如果仅导出部分模块而不导出全部模块,则必须指定所需的wictch模块

import { Module1, Module2, Module3 } from "Modules"; //grab only given in {}

if you have only export default Test you can to 如果您仅export default Test ,则可以

import "Test";

read more about modules 阅读有关模块的更多信息

when you do 当你做

 import { Tasks } from '../api/tasks.js';

you are importing Task module from '../api/tasks.js'; 您正在从“ ../api/tasks.js”导入任务模块;

when you do 当你做

import Tasks  from '../api/tasks.js';

you are importing default export module from '../api/tasks.js'; 您正在从“ ../api/tasks.js”导入默认导出模块; Here Task is a variable which is referring default export module. 这里Task是一个变量,它引用默认的导出模块。

Example. 例。

task.js export default Task; task.js导出默认Task;

case 1: It is Task from task.js case 2: It is Task variable pointing to Task module in task.js that is Task 情况1:是task.js中的Task情况2:它是Task变量,它指向task.js中的Task模块,即Task

if I do import someVariable from './api/task.js' still it will work because someVarible will point to default export module that is Task module in task.js 如果我确实从'./api/task.js'导入someVariable仍然可以正常工作,因为someVarible将指向默认的导出模块,即task.js中的Task模块

If I do 如果我做

import {someVariable} from './api/task.js' it will search for module someVariable in task.js but it is not there so now it is undefined. 从'./api/task.js'导入{someVariable},它将在task.js中搜索模块someVariable,但是它不存在,因此现在未定义。

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

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