美文网首页vue
VUE之组件全局方法

VUE之组件全局方法

作者: 陨石坠灭 | 来源:发表于2018-04-22 12:06 被阅读0次

全局方法其实是js自身就可以实现的方法,具体实现其实很简单,
比如加个日志显示组件:

export default {    
  created(){
    var _this = this
      window.error= function(msg,title){
        _this.error(msg,title)
      }
      window.info= function(msg,title){
        _this.info(msg,title)
      }
       window.debug= function(msg,title){
          _this.debug(msg,title)
      }
     },
     name:"Log",
      ...
}

调用就很简单啦:

window.error(msg,title)
//或者
error(msg,title)

是不是感觉特方便,比起那个什么总线简直强到不知哪去去了。。。

还有更加变态一点的做法:

export default {    
  created(){
      window.$log = this
  },
  methods{
      error(msg,title){
          ...
      }
  }
...
}

调用就很方便了:

$log(msg,title)

当然,别忘了把组件加入到界面,最好加入在App.vue里面:

<template>
  <div id="app">
    <div class="content">       
            <router-view/>
            <Log />
    </div>    
  </div>
</template>
<script>

import Logfrom '@/components/Log'

export default {
  name: 'App',
  components:{
    "Log":Log
  }
}
</script>

相关文章

  • vue UI组件开发

    1. 新建组件: Vue.component 方法用于注册全局组件,new Vue({ components: {...

  • VUE之组件全局方法

    全局方法其实是js自身就可以实现的方法,具体实现其实很简单,比如加个日志显示组件: 调用就很简单啦: 是不是感觉特...

  • 2017.11.15

    Vue.prototypevue——自定义全局方法,在组件里面使用 Vue.prototype 不是全局变量,而...

  • 2018-07-02 Vue源码解析阅读笔记

    合并配置 在new Vue()初始化时,会通过mergeOptions()的方法,把全局变量、全局方法、全局组件等...

  • Vue总结3-组件相关

    1.定义全局组件 1.1 方法一 37-Vue组件-自定...

  • 组件化实例化过程&&编译原理

    组件化: 组件声明、注册: 全局api:initAssetRegister(Vue) 声明三个方法componen...

  • vue Vuex axios 相关资料

    vue中文文档 使用Vuex详解 vue-router 基本使用 vue全局使用axios的方法 vue 父子组件...

  • vue语法基础二-组件

    组件 Vue组件说明注册使用全局组件所有实例都能用全局组件。Vue.component(tagName, opti...

  • vue优雅使用技巧(一)

    vue优雅使用 1. 高频组件全局引入 使用Vue.use()方法弊端: 需要多次import 引入组件 使用re...

  • Vue 全局组件和局部组件

    vue、js、html文件都可以注册全局组件和局部组件 全局组件 局部组件 vue-custom-element ...

网友评论

    本文标题:VUE之组件全局方法

    本文链接:https://www.haomeiwen.com/subject/rmnqlftx.html