Novice tutorial: www.runoob.com/w3cnote/es6… \

Required for exporting filesexportorexport defaultThe command

let add= function (str) {
    let strarr = str.split(', ');
    let changenum = strarr.reduce((pre,next) = >{
        let num1 = Number(pre);
        let num2 = Number(next);     
        return Number(num1)+Number(num2);
    });
    return changenum;
};
let jiannum= function (str) {
    let strarr = str.split(', ');
    let changenum = strarr.reduce((pre,next) = >{
        let num1 = Number(pre);
        let num2 = Number(next);   
        return Number(num1)-Number(num2);
    });
    return changenum;
};

export {add,jiannum as jian};
Copy the code

Import files requiredimportThe command

<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta  name="viewport" content="width=device-width, Initial -scale=1.0"> <title>Document</title> </head> <body> <label for="nm"> < / label > < input type = "text" name = "num" id = "nm" > < button id = "add" > add < / button > < button id = "jian" > subtraction < / button > < p > < / p > <script type="module"> import * as js2 from './js2.js'; Import {add, jian} from './js2.js'; Let jiafa = document.querySelector('#add'); let jianfa = document.querySelector('#jian'); let numinp = document.querySelector('#nm'); let tag_p = document.querySelector('p'); jiafa.onclick = function () { let strnum = numinp.value; let arrnum = js2.add(strnum); if (typeof (arrnum) ! = 'number' || Object.is(arrnum, NaN)) { tag_p.innerHTML = `<span style="color: red;" <span> '; } else { tag_p.innerHTML = `<strong style="color: red;" ${arrnum}</strong> '; numinp.value = ''; }; } jianfa.onclick = function () { let strnum = numinp.value; let arrnum = jian(strnum); if (typeof (arrnum) ! = 'number' || Object.is(arrnum, NaN)) { tag_p.innerHTML = `<span style="color: red;" <span> '; } else { tag_p.innerHTML = `<strong style="color: red;" ${arrnum}</strong> '; numinp.value = ''; }; } </script> </body> </html>Copy the code

Description:

  • Method names for exports and imports can be renamed using as
  • * can be used to import all the methods of a file, but must be called after being renamed as
  • The script tag of the import file must specify type=”module”, otherwise an error will be reported.