zoukankan      html  css  js  c++  java
  • 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)是错误的
    }
    
  • 相关阅读:
    USDT与omniCore钱包
    C# 事件(第四章)
    委托进阶(第三章)
    委托入门(第二章)
    委托入门(第一章)
    asp.net页面生命周期
    在WEB程序中小心使用"ThreadStatic"
    如何在一个请求中共享数据或对象实例
    .net垃圾回收机制原理
    MVC模式简介
  • 原文地址:https://www.cnblogs.com/huihuihero/p/13946991.html
Copyright © 2011-2022 走看看