Bringing in Vue from 'vue' allows for the incorporation of 'different' Vue to diverse documents

This issue involves the interaction between Webpack, ES6 import syntax, and Vue.

Currently, I am working on a Vuex mutation that is responsible for adding a new key-value pair mykey: [] to an object within the state. To ensure reactivity, I need to use

Vue.set(state.myobj, 'mykey', [])
.

However, importing Vue using import Vue from 'vue' in my mutations.js file and utilizing Vue.set(...) does not resolve the issue; it simply does nothing. It appears that the problem lies in the fact that the instance of Vue being used is different from the one created in main.js when initializing the Vue object.

After experimentation, I have confirmed that the problem stems from how Vue is imported into mutations.js. By assigning window.MY_VUE = Vue in main.js and then using window.MY_VUE.set(...) in mutations.js, the new array behaves as intended. However, running window.MY_VUE === Vue in mutations.js results in false.

Is there a correct way to address this issue? Am I making a mistake somewhere?

As a temporary solution, I currently replace the object:

state.myobj = { ...state.myobj, mykey: [] }

The environment consists of Vue 2.4.2, Vuex 2.4.0, and Webpack 2.7.0, with no utilization of Webpack code splitting.

Answer №1

After some investigation, I pinpointed the root cause of the issue. Upon loading the application, I observed that the message "You are running Vue in development mode." was being printed twice in the console. Both instances originated from vue.runtime.esm.js:7417 but were served from different URLs (due to Webpack URLs generated while running `npm run dev`). It became evident that I had two separate `node_modules` directories located at `./` and `../../`, resulting in Vue being loaded from both locations for some reason. This inconsistency was likely caused by an error in the order of installing the npm packages. By removing both `node_modules` directories and re-running `npm install`, I successfully resolved the issue. Subsequently, I was able to effectively utilize `import Vue from 'vue'` in mutations.js, and `Vue.set(...)` functions as intended.

Despite resolving the issue, I still lack a complete understanding of the underlying npm problem and how to prevent its occurrence in the future.

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

Is it possible to increment an integer value in jQuery after obtaining the sum result?

Actually, I'm trying to extract the integer value from my input field. For example, if I enter the value 4+5, I want to display the result as 9 in a separate div. However, instead of getting the expected result, I am receiving [object Object]. I&apo ...

Issue encountered while trying to load electron-tabs module and unable to generate tabs within electron framework

I've recently set up the electron-modules package in order to incorporate tabs within my Electron project. Below are snippets from the package.json, main.js, and index.html files. package.json { "name": "Backoffice", "version": "1.0.0", "descr ...

JavaScript is not being loaded onto the client by Express

I am looking to incorporate requireJS into my client code. Here is my file structure: ProjectRoot |-server.js |-public/ |-index.html |-js/ |-app.js |-lib/ |-require.min.js |-underscore.js |-backbone.js ...

Choose dropdown choices using Node.js, MySQL, and EJS

I am looking to create a dropdown list using data from my MySQL database. Specifically, I want to populate the names from the "customer" table in the select option of my "create budgets" form using EJS. However, I am encountering issues with passing EJS da ...

Encountering ReferenceError when attempting to declare a variable in TypeScript from an external file because it is not defined

Below is the typescript file in question: module someModule { declare var servicePort: string; export class someClass{ constructor(){ servicePort = servicePort || ""; //ERROR= 'ReferenceError: servicePort is not defined' } I also attempted t ...

VueJS intricate v-if condition

Is there a way to display a button only if two specific conditions are met? I attempted to use v-if with one condition at a time: v-if="editMode" v-if="$can('customersdelete')" When using only one condition at a time, the button displays. This ...

IE8 does not show AJAX response right away

I have encountered a peculiar problem with the code below. In Internet Explorer 8, the content retrieved through xmlHttpRequest does not appear immediately after clicking the link. It only displays after moving the mouse or cursor following the click. < ...

"Encountering unpredictable errors when trying to install npm packages on

During the installation of npm on our Windows build server, we encounter intermittent failures with the following error message: error errno: -4048, error code: 'EPERM', error path: 'C:\\Users\\bamboo\\Ap ...

The Vue.js development server compiler is hitting a roadblock at 49% progress

My Vue JS project keeps getting stuck at 49% or sometimes 62% when I run npm run serve. No matter how long I wait, it never seems to move past that point. I have searched online many times for a solution to this issue, but it appears that no one else is e ...

The alert box is failing to open

When I click the button, I expect an alert box to appear but nothing happens. I'm unsure whether I need to include var before the identifier in the function signature like this: function popup(var message) <!DOCTYPE html> <html lang="en-ca"& ...

Using AngularJS to dynamically assign classes with values derived from an ng-repeat loop

I'm attempting to assign a class to a table row using ng-class based on the value of ng-repeat. I have searched online and found examples that involve calling a function. Is it possible to simply set ng-class with a value instead of using a function? ...

What is the best way to continuously loop the animation in my SVG scene?

After designing a SVG landscape with an animation triggered by clicking the sun, I encountered an issue where the animation only works once. Subsequent clicks do not result in any animation. Below is my current JavaScript code: const sun = document.getEle ...

JavaScript Asynchronous Functions Not Handling Await Calls Correctly

The two fadeInList functions control the fading animation of a continuous list split into two lines. The typeOutText function displays text and is supposed to execute List1 first, wait for it to finish, and then proceed with List2. However, after adding ke ...

Why does my JavaScript only trigger my web service request when I set a breakpoint?

Can you help me understand why my JavaScript code only calls my webservice when I set a breakpoint on the line ].getJSON, but not if I remove the breakpoint? $(function () { $("#" + @Model.BidObjectId).submit(function () { ale ...

Animating a div in jQuery by creating a duplicate

I've spent hours scouring Google and StackOverflow for a solution to this particular issue I'm facing, but haven't been able to find one. Here's the problem: I'm currently in the process of creating a shopping website. When a user ...

Revolutionize Your App with React Native's Interactive Bottom Sheet Option

Hello there! I am trying to create a FlatList with multiple items, each of which should trigger a bottom-sheet when clicked. I want to make this dynamic but I'm encountering an error. TypeError: undefined is not an object (evaluating `_this[_reactNat ...

Array filtering functions similarly to marketplace filtering tools

In order to make the filter function like a marketplace filter, I want to only see items related to the selected brand and status. For example: partners = [ 0:{ year: "2022" badge_status: "badge-success" sale_date: "01/07/2022&quo ...

Tips for inserting the <a> HTML tag into a JSON array object

I am relatively new to creating array objects and I am unsure on how to add a link within a <a> tag in a JSON file. Here is the JSON object code: powerUpList: [ { id: 8, name: 'Automate your API column type ', description: & ...

Unlocking Discord Account Information through OAuth2

Currently, I am in the process of developing a moderation bot for Discord. I am working on implementing a paid plan and as part of that, I require users to log in with their Discord account using OAuth2. This allows me to retrieve user data and identify wh ...

When a parent document is deleted, Mongoose automatically removes any references to child documents

Greetings everyone, thank you for taking the time to read my query. I am looking to remove a child object that is referenced in a parent object. Below is the structure: const parentSchema: = new Schema({ name: String, child: { type: mongoose.Schema. ...