Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

346
Views
Vue: Use Vuex store outside of component without Import Statements

In Vue 2/3, and in an ES6 environment when I want to access a Vuex store in an external JS file (outside of the component), I would normally use something like this:

// example.js

import { store } from '../store/index';

console.log(store.state.currentUser);

This works great, however, in my current environment (Rails 5 without webpack), we can't use import statements at all.

Question: Is there any way, in regular ES5 JavaScript, to access Vuex stores outside of components?

It's worth noting that I've got a successful setup of Vuex going on our frontend, we just can't access it outside of our defined Vue components.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In my Rails setup, I have this:

// app/assets/javascripts/lib/vuex/store.js

const store = new Vuex.Store({
  modules: {
    activity: activityStore,
  }
});
// application.js

//= require vue/dist/vue.min
//= require vuex/dist/vuex.min.js

$(document).ready(function () {

  Vue.use(Vuex);

});

In this instance, store is just a global javascript object. Use it the same as you would any other JS object.

As long as you're inside a JS file that is properly compiled, and your base Vue is installed properly, you can just do store.state.activity.activities, or if you have mutations, store.commit('myMutation', 'Hello test').

My specific example was a huge function calling a webhook. The webhook can take a while, and I want to send messages to the user as I get new messages.

Example:

// webhook.js

async webhookFunction() {
  // new message recieved
  store.commit('newActivity', 'Your object has been updated');
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!