关于javascript:jsimportexport

导出的根本用法

// 导出数据
export let name = 'learn';
export const age = 18;
// 导出类
export class Rect {
    constructor(width, height){
        this.width = width;
        this.height = height;
    }
}
// 导出函数
export function sum(a, b){
    return a + b;
}

导入的根本语法

import {name, age, Rect, sum} from './example.js';

// 不能给导入的绑定赋值,如 name = 'zhangsan'; 会报错

导入整个模块

import * as example from './example.js'

// example 就蕴含了js文件里的全副export内容

import export 不容许呈现在其余语句或函数内,如if里。、

改名的状况

import {sum as add} from './example.js'

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理