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