[Vuejs]-VueJS listen to foreign event inside component

0👍

The variant with arrow functions

export default 
{
  data() 
  {
    return {
        open        : false,
        notif_count : 0,
    };
  },
  mounted() 
  {
    if (typeof Intercom !== 'undefined') 
    {
        Intercom('onUnreadCountChange', (count) =>
        {
            if (count) this.notif_count = count;
        });
        Intercom('onHide', () =>
        {
            this.open = false;
        });
    }
  },
  computed: 
  {
    ...mapState({
        HIGHER  : (state) => state.intercom.higher,
    }),
  },
}

The variant with component methods

export default 
{
  data() 
  {
    return {
        open        : false,
        notif_count : 0,
    };
  },
  mounted() 
  {
    if (typeof Intercom !== 'undefined') 
    {
        Intercom('onUnreadCountChange', this.onUnreadCountChange);
        Intercom('onHide', this.onHide);
    }
  },
  computed: 
  {
    ...mapState({
        HIGHER  : (state) => state.intercom.higher,
    }),
  },
  methods:
  {
    onUnreadCountChange(count)
    {
            if (count) this.notif_count = count;
    },
    onHide()
    {
            this.open = false;
    },
  }
}

Leave a comment