vue公用函数的封装,暴露与引入

1、在src文件夹下新建utils文件夹,再在src/utils下新建utils.js

2、在utils.js中写入公用函数并暴露(暴露方式为逐一暴露和统一暴露),如下

逐一暴露:
/**
 * 公用函数111
 * @param {*} e 
 */
export function demoEnter(e){
    let etr = e.currentTarget.childNodes[0]
    console.log(etr)
}

/**
 * 公用函数222
 * @param {*} e 
 */
export function demoLeave(e){
    let lae = e.currentTarget.childNodes[0]
    console.log(lae)
}



统一暴露:
/**
 * 公用函数111
 * @param {*} e 
 */
function demoEnter(e){
    let etr = e.currentTarget.childNodes[0]
    console.log(etr)
}

/**
 * 公用函数222
 * @param {*} e 
 */
function demoLeave(e){
    let lae = e.currentTarget.childNodes[0]
    console.log(lae)
}
export {demoEnter,demoLeave} //一起暴露出去

3、函数引入及使用

单个引入:
import demoEnter from '@/utils/utils.js
多个引入
import {demoEnter,demoLeave} from '@/utils/utils.js'

调用:
sss(e){
      console.log("在这个函数里调用公用函数")
      demoEnter(e)  //调用公用函数,注:不需要再使用this。this.demoEnter(e)是错误的
}
posted @ 2020-11-09 09:35  huihuihero  阅读(3297)  评论(0编辑  收藏  举报