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)是錯誤的
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM