Collaborate and connect a single store for both vue and electron applications

Storing data within electron's main.js to display reactively in a vue window has been a bit challenging. I have a store set up in store/index.js with state and mutations, which works fine when accessed individually from electron and vue. However, the issue is that the data is not shared between these two contexts - vue does not receive the data committed within electron.

How can I successfully commit to a store from my main electron environment and ensure that the changes are reactively displayed in my vue window?

store/index.js

import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
    state: {value: ''},
    mutations: {
        set: (state, payload) => {
            state.value = payload;
        }
    }
});

main.js

import Vue from "vue";
import App from "./App.vue";
import "./registerServiceWorker";
import router from "./router";
import store from "./store";
import vuetify from "./plugins/vuetify";

new Vue({
  router,
  store,
  vuetify,
  render: (h) => h(App),
}).$mount("#app");

App.vue

<template>
  <div>
    {{ $store.state.value }}
  </div>
</template>

<script>
import store from "./store";

export default {
  data: () => ({
  }),
  created() {
    // Committing here displays the value
    store.commit("set", 'dummy1');
  }
}
</script>

background.js

'use strict'
import store from "./store";
import { app, protocol, BrowserWindow } from 'electron'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'

// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
  { scheme: 'app', privileges: { secure: true, standard: true } }
])

async function createWindow() {
  // Create the browser window.
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION
    }
  })

  if (process.env.WEBPACK_DEV_SERVER_URL) {
    // Load the url of the dev server if in development mode
    await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
  } else {
    createProtocol('app')
    // Load the index.html when not in development
    win.loadURL('app://./index.html')
  }
}

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (BrowserWindow.getAllWindows().length === 0) createWindow()
})

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', async () => {
  createWindow()
  // Committing here does not display the changes
  store.commit("set", 'dummy2');
})

// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
  if (process.platform === 'win32') {
    process.on('message', (data) => {
      if (data === 'graceful-exit') {
        app.quit()
      }
    })
  } else {
    process.on('SIGTERM', () => {
      app.quit()
    })
  }
}

It seems that there may be two separate stores that do not share their contents as expected

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using the jquery slider in conjunction with the onchange event

I have integrated a jquery slider add-on into my project that updates a value in a Linux file whenever it is manipulated. The slider is connected to a text input box, which is set as readonly and therefore always blurred. The issue I am facing is that the ...

Troubleshooting Node.JS body parsing problems

I am struggling to transmit data from one machine to another using node.js. I am facing some challenges with getting the parser to work properly. Below is my client and server code: Client.JS var request = require('request'); request.post( ...

Can you explain the meaning of this PHP syntax and why is this variable showing NaN?

Looking for question marks on Google can be quite challenging. Can someone explain this process step by step? $page = isset($_POST['page'])?$_POST['page']:"0"; I believe it means to check if $_POST['page'] is set, use that ...

What is the recommended method for obtaining the maximum suggested number for the y-axis?

How do I determine the suggestedMax value for the yAxes scale? For instance, in the provided image, it is 4000 for legend2 and 400 for legend1) Legend Label 1: https://i.sstatic.net/ZfVSz.png Legend Label 2: https://i.sstatic.net/l05ar.png var canva ...

Discovering distinct values within an array using React/js

I am a complete novice in JavaScript and ReactJS. The majority of the code below is sourced from tutorials that I am attempting to modify. Essentially, the code displays all the values from the "tag" (people, places, things, etc.) as inline li elements to ...

The challenge of populating information within a Datalist

In my code snippet, I have a JavaScript function that is used to populate a Datalist: function PopulateDropDown(facility) { $.ajax({ url: '/Rentals/Base/GetContactsForFacility?selectedFacility=' + facility, data: { facility: ...

for each item, assign a unique ink color

Hello there! Currently, I am working on a straightforward online shopping MVC application. Within this application, there is a table containing three categories: Laptops, Mobiles, and Consoles. I have created a partial view that fetches these categories fr ...

Using Node.js to extract information from a web application that necessitates logging in

I find myself in a situation where I need to gather data from a webpage that does not have an API available. The scenario is as follows: In order to access the page data, one must first log in (usually resulting in the creation of session storage/cook ...

Choosing options from an API response in a REACT-JS application

I have a Select Component in my application and I want it to automatically show the selected data once the response is received. import Select from "react-select"; <Select menuPlacement="auto" menuPosition="fixed" ...

Using ajax and node.js for a RESTful login function

I'm currently facing an issue with implementing a basic login functionality on a server and verifying the login status by sending a GET request to retrieve the user name. My setup includes a node.js server and a single page object utilizing JQuery. / ...

Having difficulty removing new or existing lines on StackBlitz

I attempted to experiment with React on StackBlitz, but I encountered a problem where I couldn't delete any lines of code. It seems that while I can add new lines of code, deleting them is not an option. Even when logging in with GitHub, the issue per ...

In a jQuery project, WebStorm marks all $-operators as "unrecognized."

Just a quick question from a beginner: I'm facing an issue where my WebStorm IDE doesn't recognize any jQuery code, even though the webpage functions correctly in the browser. Here's what I've done so far: I have installed WebStorm V ...

Enhance Your Website with Bootstrap 4 Card Image Zoom

Is it possible to add an image zoom effect to Bootstrap 4 cards? I found some inspiration at . Below are the codes I have used: <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha ...

An issue has been identified in the bubble sort algorithm causing certain numerical lists to not be properly sorted when implemented in NodeJS

I am just beginning to learn about algorithms and have started with the bubble sort. I wrote my own implementation and it seems to work well most of the time when sorting a list of numbers from 1 to 100. However, occasionally there is one random number th ...

AngularJS Event Handler Fails to Trigger

I'm currently working on a form that I need to submit using the ng-submit event through a custom Auth service. This is a snippet of the login.html (partial template) <div class='container'> <form class='form-signin' ...

"Maintaining the jQuery carousel slider above the lightbox image for a seamless user experience

Being a newcomer to jQuery, I am currently relying solely on plugins. I recently installed a carousel slider that allows manual sliding to view images accompanied by text information underneath. By clicking on "more" at the bottom of the image/text box, an ...

Utilizing External Libraries in SAPUI5 Extension Development

I am facing an issue while trying to integrate an external library into my UI5 project. Specifically, I am working with jsPDF but it could be any other library as well. I have included it in the manifest.json file in the following manner: "js": [{ ...

Endless Invocation of Promise Functions in NodeJS

Searching for a way to endlessly call functions with promises. Experimented with 2 scenarios, one successful and the other not so much. The goal of the code that failed is to retrieve data from an API and store it in a database. Currently learning about p ...

What could be causing the 400 error in my $http.post request?

I am relatively new to working with MEAN stack and I have encountered an issue that I hope someone can help me with. I am trying to implement functionality where an email is sent to a contact upon clicking a send button. I am using the SendGrid Nodejs API ...

Solving filtering issues within React using a combination of conditions

I've been struggling to selectively remove an item from my array. The current filter I'm using is removing too much. Here is the array in question: [ { "domain": "domain1.com", "slug": "moni ...