首页 > 其他分享 >export,export default,exports - 导入导出方法总结

export,export default,exports - 导入导出方法总结

时间:2023-06-30 18:24:07浏览次数:45  
标签:exports default 导出 module export import

1.export.default的使用方法

特点:

  1. export.default向外暴露的成员,可以使用任意变量来接收
  2. 在一个模块中,export default只允许向外暴露一次
  3. 在一个模块中,可以同时使用export default 和export 向外暴露成员
//export default-默认导出
const m = 100;
export default m; 

//导入
import m from './file'; //可以改变变量名,如:import a from './file';
//export defult const m = 100;// 不可以这样写
//导出
let a = 100;
let b = {};
let c = ()=>{}

export default {
  a,b,c
}

//导入
import module from './file';
console.log( module.a, module.b, module.c );

2.export的使用用法

特点:

  1. 注意:在一个模块中,export 可以向外暴露多个
  2. 注意;使用export导出的成员,必须严格按照导出时候的名称,不能自定义,来使用 {} 按需接收
  3. 注意;使用export导出的成员,如果要换个名称,可以使用 as 起别名
'use strict';
//导出变量
export const a = '100';  
 
 //导出方法
export const dogSay = function(){ 
    console.log('wang wang');
}

//导出函数
export function catSay(){
   console.log('miao miao'); 
}

//导入- export导出的是多个,需要解构取值
import { a as b, dogSay, catSay } from './file';
//或 import * as utils from './file'
//导出
const str = "export的内容";
const year = 2019;
function message(sth) {
  return sth;
}
export { str, year, message }

//导入
import {str, year, message} from './file';
//或:import * as utils from './file';

3.export和export.default混合导出

//导出
const author = "不要秃头啊";

export const age = "18";
export default author;

//导入
import author, { age } from "./name";

console.log(author, "author");
console.log(age, "age");

4.module.exports的使用方法

特点:

  1. exports 是 module.exports 的一个引用,即 exports = module.exports
  2. module.exports 和 exports 不要同时使用,指向会有问题
//导出
module.exports = { checkIDCIP };

//导入
const { checkIDCIP } = require('./utils'); //或者 import { checkIDCIP } from './utils';

exports的使用

// person.js --- 导出
var name = 'Tom';
var age = 18;

exports.getName = function() {
    return name;
}

exports.getAge = function() {
    return age;
}

// main.js --- 导入
var person = require('./person'); //或 import person from './person'

console.log(person.getName()); // 'Tom'
console.log(person.getAge());  // 18

5.区别总结

  • 其中 export default、export、import 属于ES6产物,module.exports和require属于node.js产物。

  • require:node 和 es6 都支持的引入 (CommonJS规范)

  • export / import:只有es6 支持的导出引入

  • module.exports / exports:只有 node 支持的导出 (CommonJS规范)

  • import 引入依赖包 不需要相对路径,引入其他变量,函数等需要相对路径

参考地址

https://blog.51cto.com/u_15127691/4330800

标签:exports,default,导出,module,export,import
From: https://www.cnblogs.com/domin520Jian/p/17517565.html

相关文章

  • pmm1安装部署oracledb_exporter
    ########################被监控的oracle服务器上安装##################1.下载下载地址https://github.com/iamseth/oracledb_exporter#installationoracledb_exporter.tar.gz二进制文件包该文件里只有一个可执行文件oracledb_exporter-0.5.0.tar.gz源码包,我们需要解压该包......
  • 3普罗米修斯搭建_node_exporter
    搭建node_exporter监控一.被监控宿主机,下node_exporter容器执行命令:dockerrun-d-p9100:9100-v/proc:/host/proc:ro-v/sys:/host/sys:ro-v/:/rootfs:roprom/node-exporter二.Peometheus宿主机,编辑prometheus.yml1.添加node_exporter监控配置-job_name:'node'static......
  • mac 下使用 brew 安装包报错 error: Cannot install under Rosetta 2 in ARM default
    mac下使用brew安装包报错error:CannotinstallunderRosetta2inARMdefaultprefix(/opt/homebrew)!TorerununderARMuse:arch-arm64brewinstall...Toinstallunderx86_64,installHomebrewinto/usr/local. 解决:arch-arm64brewinstallxxx ......
  • defaultdict详解
    defaultdict详解今天在看博文的时候,无意间看到了defaultdic的用法,觉得挺有意思的,分享下defaultdict是一个字典(dict)的子类,它提供了一种更方便的方式来处理缺失键(key)的情况。与普通的字典不同,defaultdict在初始化时需要指定一个默认工厂函数(defaultfactoryfunction),该函数用......
  • 教你学会使用Angular 应用里的 export declare const X Y
    摘要:exportdeclareconstX:Y语法用于在Angular应用程序中声明一个具有指定类型的常量变量,并将其导出,以便在其他文件中使用。本文分享自华为云社区《关于Angular应用里的exportdeclareconstXY的用法》,作者:JerryWang。最近做Spartacus的Angular开发时,遇到下面这种......
  • prometheus 使用 ipmi exporter 增加硬件级别监控
    prometheus监控硬件安装ipmitool并加载相应模块yuminstallipmitoolfreeipmi-ymodprobeipmi_msghandlermodprobeipmi_devintfmodprobeipmi_poweroffmodprobeipmi_simodprobeipmi_watchdog下载ipmi_exporter源码包wgethttps://github.com/soundcloud/ipmi_......
  • 【已解决】GO语言开发中调用另一个库报错 cannot refer to unexported name XXXX
    packagemainimport( "fmt" "study_gos/main/kehu")//使用了go.mod//GOPATH;D:\workspace\studys\study_gos//cd/d/workspace/studys/study_gos/src//goenv-wGO111MODULE=on//gomodinitstudy_gos//gomodtidy//跳过goget失败//go......
  • 【redis已解决】Warning: no config file specified, using the default config. In o
    1.启动redis:双击redis-server.exe。报错:Warning:noconfigfilespecified,usingthedefaultconfig.InordertospecifyaconfigfileuseF:\liuf\Redis3\redis-server.exe/path/to/redis.conf 2.解决方法一:点击这个报错表示没有指定配置文件,使用默认配置。要指......
  • mysql - #1067 - Invalid default value
    mysql中无法设置默认值为函数或者表达式,如果你强制设置时,就会报错误:#1067-Invaliddefaultvalue。这不是mysql的bug,而是故意这么设计的。参看:http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html这里写道:"TheDEFAULTvalueclauseinadatatypespecification......
  • prometheus 监控 hadoop + Hbase + zookeeper + mysql exporter
    1. run JMX exporter as a java agent with all the four daemons. For this I have added EXTRA_JAVA_OPTS in hadoop-env.sh and yarn-env.sh :[root@cloud01hadoop]#catyarn-env.sh|egrep-v'^$|#'exportYARN_RESOURCEMANAGER_OPTS="$YARN_RESOURC......