Vue挂载自定义方法

文章介绍了两种在Vue应用中实现全局方法的方法:一是通过Vue.prototype在main.js中直接添加自定义函数;二是编写公共文件,然后在main.js中导入并使用Vue.use()挂载。这两种方式都能使得所有组件能调用同一功能,提高代码复用性。
摘要由CSDN通过智能技术生成

方法一Vue.prototype:

        main.js加入

        Vue.prototype.方法名 = { 功能 } 直接在Vue构造函数中添加此方法,后续可以所有组件调用this.internalFolding(folding) 

//Vue 全局挂载自定义函数
//folding为传进来的参数
//internalFolding 为函数名
 
Vue.prototype.internalFolding = function (folding){
    //函数内容
}

方法二:写好自己需要用得公共文件

        main.js加入公用文件

import folding from '../static/js/folding'  // 方法名和导入得公用文件路径
Vue.use(folding);

        后续所有组件可以调用 this.internalFolding(this.folding) 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值