美文网首页
export、import

export、import

作者: 江湖小盛 | 来源:发表于2022-12-16 15:49 被阅读0次
    // 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
    

    相关文章

      网友评论

          本文标题:export、import

          本文链接:https://www.haomeiwen.com/subject/cjurqdtx.html