在VUE的export default外部调用methods方法

655 阅读1分钟

需求

在export default的外部Function中调用内部的methods方法

解决方案

<template>
...
</template>

<script>
import XYClient from "@/utils/webRtc/main"
var client = XYClient.DispatcherClient.getInstance();
client.serverConnectStateChanged.on((state) => {
  var stateStr
  switch (state) {
      case 1:
        stateStr = "connected";
        break;
      case 0:
        stateStr = "disconect";
        break;
      default:
        break;
   }
   that.mylog("连接服务状态改变: " + stateStr); // 在此处调用methos中的方法
});
var that = null // 声明一个变量作为媒介
export default {
    methods:{
        mylog(msg){ // 需要调用的method方法
          msg = this.getNowFormatDate() + " : " + msg + "\r\n";
          this.output = msg + this.output;
        }
    },
    created:function(){
        that = this
    }
} 
</script>