方法一:使用Vue.prototype

//在mian.js中写入函数

Vue.prototype.getToken = function (){
...
}

//在所有组件里可调用函数

this.getToken();

方法二:使用exports.install+Vue.prototype

// 写好自己需要的fun.js文件

exports.install = function (Vue, options) {
Vue.prototype.getToken = function (){
...
};
};

// main.js 引入并使用

import fun from './fun'
Vue.use(fun);

//在所有组件里可调用函数

this.getToken();

在用了exports.install方法时,运行报错exports is not defined

解决方法

export default {
install(Vue) {
Vue.prototype.getToken = {
...
}
}
}

方法三:使用全局变量模块文件

Global.vue文件:

<script>
const token='12345678'; export default {
methods: {
getToken(){
....
}
}
}
</script>

在需要的地方引用进全局变量模块文件,然后通过文件里面的变量名字获取全局变量参数值。

<script>
import global from '../../components/Global'//引用模块进来
export default {
data () {
return {
token:global.token
}
},
created: function() {
global.getToken();
}
}
</script>

Vue自定义函数挂到全局方法的更多相关文章

  1. 转载 jQuery和js自定义函数和文件的方法(全网最全)

    jQuery和js自定义函数和文件的方法(全网最全)    版权声明:本文为像雾像雨又像风_http://blog.csdn.net/topdandan的原创文章,未经允许不得转载. https:// ...

  2. excel自定义函数添加和使用方法

    第一,excel自定义函数简介 Excel自带很多函数供使用,但有些问题用内置函数解决起来很复杂,甚至是无能为力,这时就可以利用VBA开发自定义函数. 第二,excel如何添加自定义函数 excel自 ...

  3. Vue自定义过滤器

    gitHub地址: https://github.com/lily1010/vue_learn/tree/master/lesson05 一 自定义过滤器(注册在Vue全局) 注意事项: (1)全局方 ...

  4. smarty 自定义函数

    自定义函数:<{方法名称}> 在lib/plugins中新建文件,命名方式是固定的:function.方法名称.php 或者 block.方法名称.php 1.<{literal}& ...

  5. 11 vue 自定义全局方法

    //global.js// 定义vue 全局方   // 定义vue 全局方法 建议自定义的全局方法加_ 以示区分 export default {   install(Vue, options =  ...

  6. vue 自定义全局方法

    import {myfun} from '../static/js/test.js' //se6的正确写法export default {methods:{ diyfun:function () { ...

  7. vue---vue2.x自定义plugin,给vue添加全局方法,原型上增加全局方法

    1. 自定义plugin.js export default{ install(Vue,options); { Vue.prototype.toStringTwo=(str)=>( ('0000 ...

  8. Vue自定义指令使用方法详解 和 使用场景

    Vue自定义指令的使用,具体内容如下 1.自定义指令的语法 Vue自定义指令语法如下: Vue.directive(id, definition) 传入的两个参数,id是指指令ID,definitio ...

  9. vue定义全局方法 调用其他组件的方法

    官网的写法  vue实例.$on就可以在根实例上定义全局方法 this.$root就是获取根实例  如果没有根实例 就表示当前实例 this.$root.$on 不需要.eventHub 不需要下面这 ...

随机推荐

  1. CKEditor高级编辑器

    是否感觉后台分类描写叙述信息.商品描写叙述信息以及文章描写叙述信息 编写时很的不方便?有时候会将word的格式也复制过来了?那这个插件就比較适合了. 本插件使用CKEditor最新版本号,复制过来的内 ...

  2. To new is C++; To malloc is C; To mix them is sin (混淆C++中的new和C中的malloc是一种犯罪)

    Introduction One of the most common questions that get asked during interviews for C++ programmers i ...

  3. Lesson 1 Basic Concepts: Part 2

    Getting your web site ‘live’ on the Web With the nerd background details under our belts, we can now ...

  4. TFRecord —— tensorflow 下的统一数据存储格式

    tensorflow 提供了统一的数据存储格式,即 TFRecord(record 表示记录),以提高程序的可扩展性,当数据来源十分复杂时,仍能有效记录输入数据中的信息. 1. tfrecord 使用 ...

  5. 团队作业——团队项目Alpha版本发布

    该作业所属课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 作业要求链接    https://edu.cnblogs. ...

  6. Android开发当中Parcelable接口的使用

    本文转载于:http://www.2cto.com/kf/201205/132814.html 本文稍微做了些修改 android提供了一种新的类型:Parcel.本类被用作封装数据的容器,封装后的数 ...

  7. 客户端本地存储(cookie、web Storage、vuex)选择

    一.cookie .localStorage .sessionStorage .vuex 比较 cookie   4K    有时效性    可服务器传递 cookie是由服务器产生,存储在客户端的一 ...

  8. 【Django】Cookie

    目录 Cookie介绍 操作Cookie 获取Cookie 设置 Cookie 删除Cookie @ Cookie介绍 Cookie的由来 大家都知道==HTTP协议是无状态的==. ==无状态的的意 ...

  9. CSUOJ 1532 JuQueen

    Problem H JuQueen JuQueen is the super computer with the best performance allover Germany. It is on ...

  10. Zookeeper vs. etcd

    etcd是go语言实现的. 对比,可以参考这篇文章: http://studygolang.com/articles/4837 <服务发现:Zookeeper vs etcd vs Consul ...