当前位置:网站首页>Modular import and export collation in JS

Modular import and export collation in JS

2022-06-22 07:42:00 Huangbao ~

Reference resources 1

Reference resources 2

  • module.exports And exports yes CommonJS The specification of
  • export And export default yes es6 standard
  • require yes AMD Standard introduction method
  • import yes es6 A grammar standard of

Applets can also be used import xx ,../ On behalf of the previous Directory ,./ Represents the current directory ,

newly build   commonUtils.js file  

export const getTest=()=>{
return ' Hello world ~'
}


export const getTest2= function(){
  return ' Hello world '
}


export  const B =' I am exporting export';

function  A(){
  return 'Addd';
}
function  AA(){
  return 'AA';
}
export const a ='1';

let dd= function(){
  return 'aaaa';
}

//es6  The default is derived 
export default {dd:dd,AA:AA}; 


// Unified export 
export{
  A
  ,AA
}


export class User{
  constructor(){
    this.username = '';
     this.age = '';
  }
  print(){
    console.log(" Start ");
    console.log(" I'm a class user How to open it  "+this.username);
    console.log(" end ");
  }
}

// Use   module.exports  after   above export All the failure 

// module.exports={
//   A:A
//   ,A:AA
// }

establish index.js  introduce

 Import commonUtils All exports in 
const commonUtils = require('../../utils/commonUtils.js');

 Just introduce... As needed  getTest  You can use it directly ,getTest()
import { getTest } from '../../utils/commonUtils.js'

 introduce  export defaut  The object of  ,getTest  Represents the introduced object 
import getTest from '../../utils/commonUtils.js'


 Just introduce... As needed  User  You can use it directly ,User
import { User } from '../../utils/commonUtils.js'

Page({
  data: {

  },
 
  onLoad() {

    console.log("commonUtils")
    console.log(getTest);
//  let d =commonUtils.aaaa;
//     console.log(d());
    let user = new User();
    user.username =' boss Huang ';
    user.print();
    
    let user2 = new User();
    user2.username =' Guibiwei  ';
    user2.print();

    console.log("aaaaaaa1",user == user);
    console.log("aaaaaaa2",user == user2);
    // this.setData({
    //   hello:commonUtils.getTest()
    // })
  }
})

原网站

版权声明
本文为[Huangbao ~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220534226206.html