Tips for maintaining reactivity in nested object properties in external JavaScript class files without relying on Vue.set()

In my possession is this particular component:

<template>
    <div class="simple-editor">
        {{editor.view.toolbarManager.buttons}}
        <component
            v-for="(button, name) in editor.view.toolbarManager.buttons" 
            :is="button.component" 
            :options="button.options"
            :key="name"
        ></component></div>
//.......................

I am attempting to incorporate

editor.view.toolbarManager.buttons
into a v-for loop. According to Vue devtools, the object
editor.view.toolbarManager.buttons
has 4 properties which each contain another object.

<script>
export default {
    data: function() {
    return {
        editor: new Editor({
            doc: this.value,
            init: this.init,
        }),
    }
    },

The values of

editor.view.toolbarManager.buttons
are set within subclasses of the Editor() class through dynamically imported scripts like so:

props.plugins.forEach(plugin => {
    this.plugins[plugin] = import(/* webpackMode: "eager" */ '../plugin/' + plugin);
});

To populate

editor.view.toolbarManager.buttons
, I use the following methods:

// case 1: functions as intended
Vue.set(this.buttons, name, {
    component,
    options,
});     

/* case 2: loses vue reactivity
var button = {};

button[name] = {
    component,
    options,
};

Object.assign(this.buttons, button);
*/

/* case 3: loses vue reactivity
this.buttons[name] = {
    component,
    options,
};
*/

The problem arises when attempting to render

{{editor.view.toolbarManager.buttons}}
within the template - for cases 2 and 3, an empty object is displayed like so:

{}

This indicates that the Vue reactivity is disrupted. Since Editor() is an external class that I prefer not to bind with Vue, maintaining reactivity using splice/push methods commonly used with arrays is not feasible. Are there similar methods available to maintain Vue reactivity with object properties?

Answer №1

Oops! I made a mistake with using Object.assign(). Here is the correct way to use Object.assign() instead of Vue.set():

var item = {};

item[type] = {
    element,
    parameters,
};

this.items = Object.assign({}, this.items, item);

This method works perfectly for me. You can find more information in the documentation here https://v2.vuejs.org/v2/guide/reactivity.html#For-Objects:

// Instead of `Object.assign(this.someObject, { a: 1, b: 2 })`
this.someObject = Object.assign({}, this.someObject, { a: 1, b: 2 })

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

Leveraging Javascript Modules within a Typescript Vue Application

The issue at hand I've encountered a problem while attempting to integrate https://github.com/moonwave99/fretboard.js into my Vue project. My initial approach involved importing the module into a component as shown below: <template> <div&g ...

Upon executing npm install in my Laravel project, an error message promptly appears stating: "npm WARN deprecated [email protected]: gulp-util is deprecated - replace it."

Recently, I set up a brand new Laravel Project. After executing the command: php artisan preset vue, when I tried running npm install, an error occurred that stated: npm WARN deprecated [email protected]: gulp-util is deprecated - replace it, followi ...

unable to transfer Vuex store information to the graph

Currently, I am facing an issue with passing my vuex store data into my apexcharts graph. Despite my efforts, it seems like the data is not being displayed correctly. Can anyone provide guidance on what I might be doing wrong? My main objective is to updat ...

Utilizing Nicknames in a JavaScript Function

I'm dealing with a function that is responsible for constructing URLs using relative paths like ../../assets/images/content/recipe/. My goal is to replace the ../../assets/images section with a Vite alias, but I'm facing some challenges. Let me ...

Vue CLI Plugin Electron Builder displays a completely empty screen after compiling

After building my electron app using this specific plugin, I encountered a frustrating issue where the installed package would only display a blank, white screen. Despite setting up the window to open dev tools in the built version, inspecting the page rev ...

Node.js vulnerability labeled as CVE-2021-38628

Description: Enhance security measures by disabling the use of TLSv1.0 protocol and transitioning to a more secure option like TLSv1.2. To perform a manual test, utilize the following openssl commands: openssl s_client -connect ip:port -tls1. If succes ...

Issue encountered: Trying to deploy firebase functions and hosting with vue-cli v3 and node.js leads to an error "No npm package found in functions source directory

My plan is to utilize Vue.js for the Frontend and Firebase Functions (Express.js) + Firestore for the Backend. Step 0: I initiated a new project on Google Firebase, then created a new Service Account with Owner's permissions to be used with Admin SDK ...

Is there a way to send a post request containing a base64 encoded image?

I am currently developing an image upload component in Vue.js that includes a custom cropping option. The cropped version of the image is saved in my state as a base64 string, like this: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAeAAAAHgCAYAAAB91L6 ...

Guide to utilizing Terser in conjunction with webpack

I am currently using Webpack 6.10.2 in combination with Vue 3.9.3. I encountered an issue with Uglify.js when running npm run build due to its inability to handle ES6 code. To work around this problem, I followed the recommendation of removing Uglify fro ...

When initializing a Vue application using the command 'vue create hello-world', the './fs' module is not found

After spending the last 3-4 hours trying to work with Vue, I'm finding it incredibly challenging just to get it up and running. I've been following the documentation provided here: https://cli.vuejs.org/guide/creating-a-project.html#vue-create ...

What are the steps to crawl and save content from multiple websites simultaneously?

My project involves creating a webpage that can crawl multiple other webpages and integrate the data. I am using node.js & vue.js for this purpose. By utilizing puppeteer & cheerio, I have successfully been able to crawl a single page and store its data i ...

Encountering "npm WARN optional dep failed, proceeding with fsevents" error while attempting to install vue-cli on AWS/EC2 instance

I'm in the process of setting up vue-cli on AWS. Everything seems good with my permissions, and I have node v4.4.5 installed. Upon executing npm install --global vue-cli, the cursor flashes for approximately 30 seconds before displaying this error m ...

What causes the ignoring of config.proxy in axios requests while working on a webpack project?

My objective I am aiming to make a request using [email protected] with full efficiency through an http proxy (squid). My project is built on Vue and uses the webpack template. I initialized it with vue init webpack proxytest The challenge Despite ...

The system was unable to locate node.js with socket.io

I'm having trouble locating the file. According to the documentation I reviewed, socket.io is supposed to be automatically exposed. Encountered Error: polling-xhr.js?bd56:264 GET http://localhost:8081/socket.io/?EIO=3&transport=polling&t=LyY ...

Exploring the possibilities of updating the version dynamically in package.json file

Upon receiving version 1.0.1 (server_version) from the server I make sure process.env.version matches server_version If they are the same, I update process.env.version to server_version. But unfortunately, this process cannot be done from the client side. ...

How can I retrieve server-side data in the client-side using NodeJs (connecting to MySql) with Vue CRUD?

Currently, I am delving deeper into Vue and to make the learning process more engaging, I have established a connection to my MySql-DB using nodeJS. Following a detailed tutorial (), I successfully implemented a Login system. Now, my goal is to retrieve d ...

`Finding the nodejs API route for displaying images from a database`

How can I successfully display an image from the database without getting a string of question marks instead? Click here to see the issue >>> When attempting to directly call the API using the provided link, the following result is returned: {&qu ...

Converting a string into an array of JSON objects

I am currently attempting to send data to my NodeJS server using the HTTP protocol (vue-resource). The goal is to send an array of JSON objects structured like this : [{"name":"Charlotte","surname":"Chacha","birth":"2000-04-02"},{"name":"Michael","surname ...

The installation of npm was unsuccessful due to an error: npm encountered an invalid JSON response

Warning: Peer dependency overridden by npm. Found: [email protected] in node_modules/@ivan/data-insights/node_modules/bootstrap. Bootstrap@^3.3.7 from @ivan/[email protected] version 1.7.26. Could not resolve dependency because peer bootstrap@^4.3.1 nee ...

Encountering an error while setting up the object spread operator Babel plugin for ES201

Exploring the possibilities of the new ES2018 spread operator for objects led me to discovering a promising NPM package: babel-plugin-transform-object-rest-spread Here's a glimpse of my package.json: // Scripts section "scripts": { "dev": " ...