javascript - 如何在组件方法中具有可访问的变量

标签 javascript vue.js sinch

我正在开发一个组件,负责将我的用户注册到 Sinch(VoIP 平台)。为了让我的注册工作,我需要有一些可以通过我的组件方法访问的变量。我想知道如何使用 vue 来完成此操作。

我需要在我的方法 newUserRequest()loginRequest() 中访问我的变量 sinchClient

有什么建议吗?

正弦变量

var sinchClient = new SinchClient({
  applicationKey: "My-Key",
  capabilities: {
    messaging: true,
    calling: true
  },
  supportActiveConnection: true,
  onLogMessage: function(msg) {
    console.log(msg);
  }
});

方法

<script>
export default {
  data() {
    return {
      username: null,
      name: null,
      password: null,
      loggedIn: false
    };
  },
  mounted() {},
  methods: {
    newUserRequest() {
      console.log(this.name, this.password);

      if (this.name && this.password) {
        var handleSuccess = () => {
          console.log("User created");
          this.loggedIn = true;
          this.name = sinchClient.user.userId;
        };
        var handleFail = error => {
          console.log(error.message);
        };

        var signUpObject = { username: this.name, password: this.password };
        sinchClient
          .newUser(signUpObject)
          .then(sinchClient.start.bind(sinchClient))
          .then(() => {
            localStorage[
              "sinchSession-" + sinchClient.applicationKey
            ] = JSON.stringify(sinchClient.getSession());
          })
          .then(handleSuccess)
          .fail(handleFail);
      }
    },
    logInRequest() {
      if (this.name && this.password) {
        var handleSuccess = () => {
          console.log("User logged in");
          this.loggedIn = true;
          this.name = sinchClient.user.userId;
        };
        var handleFail = error => {
          console.log(error.message);
        };
        var signUpObject = { username: this.name, password: this.password };
        sinchClient
          .start(signUpObject)
          .then(() => {
            localStorage[
              "sinchSession-" + sinchClient.applicationKey
            ] = JSON.stringify(sinchClient.getSession());
          })
          .then(handleSuccess)
          .fail(handleFail);
      }
    }
  }
};
</script>

最佳答案

您可以全局定义sinchClient并使用窗口(window.sinchClient)访问它。更好的是,您可以创建一个 Vue 插件并将其注入(inject)应用程序上下文中:

var sinchClient = new SinchClient({
  applicationKey: "My-Key",
  capabilities: {
    messaging: true,
    calling: true
  },
  supportActiveConnection: true,
  onLogMessage: function(msg) {
    console.log(msg);
  }
})
Vue.use({
  install: function(Vue) {
    Object.defineProperty(Vue.prototype, '$sinchClient', {
      get () { return sinchClient }
    })
  }
})

并在 Vue 上下文中使用 this.$sinchClient 访问它

关于javascript - 如何在组件方法中具有可访问的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54030037/

相关文章:

javascript - 注销后如何在不使用浏览器缓存的情况下从服务器重新加载 Angular 应用程序

javascript - Microstrategy 使用可视化作为选择器 D3 服装图表

ios - iOS 中使用 Sinch 客户端调用电话不显示来电号码

javascript - Vue.js 列表 orderBy 过滤器不对动态添加的项目排序到数组

javascript - Vue.js - 如何与子组件共享父属性?

ios - Sinch演示中的SINUIViewController有什么用?

ios - 未通过网络应用程序从 Sinch 收到推送通知

javascript - 使用 Ajax 请求执行 PHP 文件

javascript - 将数据放在 firebase 数据库中多个位置的最佳方法

vue.js - Vue-cli 3 - Material 设计图标安装