js export 对外输出常量、变量和函数

目录

输出常量 / 变量

单独输出

一起输出

输出函数


输出常量 / 变量

单独输出

单独输出常量 

export const webName = 'SunShine 编程俱乐部';

 单独输出变量

export var year = 1958;

在其他文件中使用

// 一个变量时
    import {year} from './utils/common.js'

// 多个变/常量时

    import {year,webName} from './utils/common.js'
            console.log(year)
            console.log(webName)

一起输出

用 { } 包裹,一起输出(推荐)

const webName = 'SunShine 编程俱乐部';
var year = 1958;
export default {webName, year};

在其他文件中使用

import data from './utils/common.js'
console.log(data.year)
console.log(data.webName)

输出函数

src/plugins/SUI/utils/common.js

// 计算文本长度
export function getLength(content) {
    if (typeof content === 'string') {
        //先把中文替换成两个字节的英文,再计算长度
        return content.replace(/[\u0391-\uFFE5]/g, "aa").length
    }
}

 在其他文件中使用

    import {getLength} from './utils/common.js'
let contentLength = getLength(content)

猜你喜欢

转载自blog.csdn.net/weixin_41192489/article/details/111499735