vue中全局公共方法和变量的使用


1.在assets文件夹下,新建js文件夹,创建common.js

export default {    
    istest(){
        console.log("this is test")
    }
}

2.如果是全局(多页面)使用:在main.js中引入

/* 引入公共js*/
import common from '@/assets/js/common.js'
Vue.prototype.common=common;

3..在VUE文件中使用

this.common.istest();    //this is test
如果是单页面使用:

1.在vue的script中引入

import common from '@/assets/js/common'

2.在vue文件中使用

common.istest();    //this is test

代码示例:

全局引入文件

1.先定义共用组件 common.vue

2.需要使用的地方导入