// 1、默认导出变量
const test = 'hellow world'
export default test
import testStr from './test.js'
console.log(testStr) // hellow world
// 2、默认导出函数
const request = (params) => {
return {
name: 'zhangsan',
age: 10
}
}
export default request;
import requestFn from './test.js'
console.log(requestFn()) // {name: 'zhangsan', age: 10}
// 3、默认导出对象
const page = 10;
const currrent = 0;
export default {
page,
currrent
}
import pageObj from './test.js'
console.log(pageObj) // {page: 10, currrent: 0}
// 1、多个导出
export function test1() {
console.log('tst1')
}
export function test2() {
console.log('tst2')
}
import { test1, test2 } from './test.js'
// 2、多个导出合并写法
function test1() {
console.log('tst1')
}
function test2() {
console.log('tst1')
}
export {
test1,
test2
}
import { test1, test2 } from './test.js'
// 3、再导出
function test1() {
console.log('tst1')
}
function test2() {
console.log('tst1')
}
export {
test1,
test2
}
// 导入区块代码
export { test1, test2 } from './test.js'
// 上面代码等同于
import { test1, test2 } from './test.js'
export { test1, test2 }
// 4、导出所有
export function test1() {
console.log('test1')
}
export function test2() {
console.log('test2')
}
// 中间文件index.js
export * from './test.js'
export * from './test2.js'
import { test1, test2 } from './index.js'
console.log(test1()) // test1
网友评论