简体   繁体   English

如何在Action Files Vue JS中使用插件

[英]How to use plugins in Action Files Vue JS

We are using https://github.com/mazipan/vue2-simplert-plugin 我们正在使用https://github.com/mazipan/vue2-simplert-plugin

In our Main.js 在我们的Main.js中

import Simplert from 'vue2-simplert-plugin'
Vue.use(Simplert)

In the xxx.vue file, we can call 在xxx.vue文件中,我们可以调用

var infoMessageBox= {
  message: "",
  type: 'info',
  onClose: this.onClose
};
this.$Simplert.open(infoMessageBox);

This can work normally. 这可以正常工作。

Now the tricky part comes. 现在棘手的部分来了。 We will call dispatch (Action file) a method. 我们将调度(动作文件)称为方法。 Below code is example: 下面的代码是示例:

var requestMessage = {
  customerId: "",
  accessToken: "",
  store: this.$store,
  redirect: true,
  insideThis: this,
  network: "",
  alertbox: this.$Simplert,
};
this.$store.dispatch('checkIfCustomerExists', requestMessage);

Here is the action file 这是动作文件

import Vue from "vue";

export const checkIfCustomerExists = (context, request) => {
  var infoMessageBox= {
    message: "",
    type: 'info',
    onClose: this.onClose
  };
  request.alertBox.open(infoMessageBox)
};

Above code will work and show the alert. 上面的代码将起作用并显示警报。 This will work because I passed the this.$Simplert as parameter. 这将起作用,因为我通过了this。$ Simplert作为参数。

But I dont want to pass everytime to the parameter. 但是我不想每次都传递给参数。 I want to access it somehow inside this action file. 我想以某种方式在此操作文件中访问它。 I need to do some import or something like that?? 我需要进行一些导入或类似的操作?? How can I use this Simplert without sending it as parameter. 如何在不将其作为参数发送的情况下使用此Simplert。

Update 1: Suggestion is to use bus event. 更新1:建议使用总线事件。 I created a file event-bus.js 我创建了一个文件event-bus.js

  import Vue from 'vue';
  export const EventBus = new Vue();

In the Vue file I do emit: 在Vue文件中,我发出了:

  import { EventBus } from '../../../../store/event-bus.js';


EventBus.$emit('i-got-clicked',this.$Simplert);

var requestMessage = {
  customerId: "",
  accessToken: "",
  store: this.$store,
  redirect: true,
  insideThis: this,
  network: "",
  alertbox: this.$Simplert,
};
this.$store.dispatch('checkIfCustomerExists', requestMessage);

However in the action file. 但是在动作文件中。

  import { EventBus } from '../../../../store/event-bus.js';

  EventBus.$on('i-got-clicked', Simplert => {
    console.log("adsasda");
    Simplert.open(infoMessageBox);
});

console is not printing. 控制台无法打印。 So I suppose this is not active. 所以我想这是不活跃的。

Seems the plugin already uses an event bus for triggering the open / close events so you should be able to tap in to that via the plugin instance registered into Vue 似乎插件已经使用事件总线来触发打开/关闭事件,因此您应该能够通过注册到Vue的插件实例来访问该事件

In your store, try 在您的商店中,尝试

import Vue from 'vue'

// snip

export const checkIfCustomerExists = (context, request) => {
  // snip
  Vue.prototype.$Simplert.open(infoMessageBox)
}

Note, this seems pretty hacky to me. 请注意,这对我来说似乎很棘手。 I'd advise you to raise a feature request with the maintainers to expose the SimplertEventBus so it can be used globally. 我建议您向维护人员提出功能请求,以公开SimplertEventBus以便可以在全球范围内使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM