es 6 export import

es 5 导出 export 

单个导出

// 导出变量
export const d = "吼吼";

单个导入

import { a } from "./assets/js/exporttest";
console.log("a", a);

单个导出 变量 和函数

export const a = "你好";
export const testA = function (msg) {
  console.log(msg);
};

单个导入变量函数 需要使用 { }

import { a, testA } from "./assets/js/exporttest";
console.log("a", a);
console.log("testA", testA("你好世界"));

 

 

 

// 导出函数
export const testA = function (msg) {
  console.log(msg);
};

 

//导出默认

const c = "哈哈";
export default {
  c,
};

默认导入

import test from "./assets/js/exporttest";
console.log("test", test.c);

 

混合导出 既有单个 又有默认

export const a = "你好";
export const testA = function (msg) {
  console.log(msg);
};

export default {
  c: "世界",
};

混合导入 默认的要写在前面 c ,单个导入的 要用 {a,testA}

import c, { a, testA } from "./assets/js/exporttest";
console.log("a", a);
console.log("testA", testA("你好世界"));
console.log("c", c.c);

导入所有 并且 重新命名 这里 注入 默认 导出的  导入 时 用 default

import * as test1 from "./assets/js/exporttest";
console.log("a", test1.a);
test1.testA("你好世界")
console.log("c", test1.default.c);

存在混合导出时候 导入 这么 写 import test1 from "./assets/js/exporttest"; 这是导入默认的 ,单个导出 是不能导入的

import test1 from "./assets/js/exporttest";

console.log("a", test1);
test1.testA("你好世界")
console.log("c", test1.default.c);

 

 

export const a = "你好";
export const b = "很好";
// 导出变量
export const d = "吼吼";
// 导出函数
export const testA = function (msg) {
  console.log(msg);
};
class User {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
}
export const XiaoMing = new User("小明", 12);
const c = "哈哈";
export default {
  c,
};

 

//混合导入 test

import testA, { a, XiaoMing } from "./assets/js/exporttest";
console.log("testA", testA.c);
console.log("a", a);
console.log("xiaoming", XiaoMing.name);

//混合导入 命名方式 需要写 default

import * as testA from "./assets/js/exporttest";
console.log("c", testA.default.c);
console.log("f",testA.default.f)
console.log("a", testA.a);
console.log("xiaoming", testA.XiaoMing.name);

 

posted on 2024-09-24 10:12  是水饺不是水饺  阅读(25)  评论(0)    收藏  举报

导航