export、import數組
使用export、import語法,記得要進行babel編譯babel
// A.js export const name = 'hello' // B.js import {name} from './A' console.log(name) // hello
// A.js export const name = 'hello' export let addr = 'beijing' // B.js import { name, addr } from './A' console.log(name, addr) // hello beijing
// A.js export const list = [1, 2, 3] // B.js import { list } from './A' console.log(list) // [1, 2, 3]
// A.js const name = 'hello' let addr = 'beijing' const list = [1, 2, 3] export { name, addr, list } // B.js import { name, addr, list } from './A' console.log(name, addr, list) // hello beijing [1, 2, 3]
只能有一個默認導出,其餘的都要放如對象後面去函數
// A.js const name = 'hello' let addr = 'beijing' const list = [1, 2, 3] export default name export { addr, list } // B.js import name, { addr, list } from './A' console.log(name, addr, list) // hello beijing [1, 2, 3] // C.js // 若是想要直接更名字,須要默認導出的變量才行 import name2 from './A' console.log(name2) // hello // D.js // 不是默認導出的變量,修更名字就須要用as import { addr as addr2 } from './A' console.log(addr2) // beijing
// A.js export function say (content) { console.log(content) } // B.js import {say} from './A' say("helloword") // helloword
默認導出學習
// A.js export default { code: 1, message: 'success' } // B.js import obj from './A' console.log(obj)
變量形式this
// A.js const data = { code: 1, message: 'success' } const info = { name: 'xm', age: 16 } export default { data, info } // B.js import { data, info } from './A' // 報錯,引入衝突 // 只能總體導入再結垢賦值 import obj from './A' let { data, info } = obj console.log(data, info) // {code: 1, message: "success"} {name: "xm", age: 16}
// A.js class Test { constructor () { this.id = 2 } } export { Test } // B.js import {Test} from './A' let test = new Test() console.log(test.id) // 2
orspa
export class Test { constructor () { this.id = 4 } }
默認導出類code
// A.js class Test { constructor () { this.id = 3 } } export default Test // B.js import Test from './A' let test = new Test() console.log(test.id) // 3
or對象
export default class Test { constructor () { this.id = 4 } }
orblog
// 若是是default就不用寫類名了,沒有default必須寫類名 export default class { constructor () { this.id = 5 } }
// A.js class Test { constructor () { this.id = 3 } } class Ani { constructor () { this.name = 'animal' } } export { Test, Ani } export default class People{ constructor () { this.id = 123 } } // B.js import * as Mod from './A' let test = new Mod.Test() console.log(test.id) // 3 let ani = new Mod.Ani() console.log(ani.name) // animal // 只能用default來獲取默認的類,不能使用People let people = new Mod.default() console.log(people.id) // 123