Attempting to modify an object's property while maintaining reactivity, unfortunately encountering an issue with the property or method "vm" not being defined on the instance despite being referenced during the render

Up to this point, I've been attempting to set the value of an object property to false using @click='review.isActive = true'. However, it appears that Vue cannot detect these changes, causing the property to not be reactive.

According to the documentation found at https://v2.vuejs.org/v2/guide/list.html#Caveats, there are alternative methods to avoid this issue:

Vue.set(vm.reviews, index, true)

vm.reviews.splice(index, 1, true)

Unfortunately, I encountered errors when trying both approaches. It seems like I lack a full understanding of how the Vue instance operates and may be attempting to access it incorrectly.

I presume that Vue/vm refer to the Vue instance, but for some reason, I can't seem to interact with it.

In both instances, I receive the error message "Property or method 'vm'/'Vue' is not defined on the instance but referenced during render."

Using the Vue webpack-simple webpack template, my Vue instance is initialized in the main.js file as shown below:

var vm = new Vue({
    el: '#app',
    router: router,
    store: store,
    render: h => h(App)
});

The array manipulation takes place in a different file within a component.

Answer №1

In the scenario where your review object exists within your reviews array and does not have an isActive property initially, you can utilize vm.$set to introduce a new reactive property.

Consider this example:

new Vue({
  el: '#app',
  data: {
    reviews: [{
      id: 1,
      name: 'Review #1'
    },{
      id: 2,
      name: 'Review #2'
    },{
      id: 3,
      name: 'Review #3'
    }]
  }
})
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<div id="app">
  <div v-for="review in reviews" :key="review.id">
    <p>
      Review {{ review.name }} status: 
      <code>{{ review.isActive ? 'ACTIVE' : 'INACTIVE' }}</code>
    </p>
    <button @click="$set(review, 'isActive', true)">Activate</button>
  </div>
</div>

Observe how $set is employed in the @click event handler.


As mentioned in the comment below, if you wish to append new properties post data retrieval, ensure it is done prior to assigning the value to your data property, for instance

this.reviews = data.from.somewhere.map(review => ({...review, isActive: false}))

If there arises a necessity to add a new property after data assignment, you must employ vm.$set to render it reactive

this.reviews.forEach(review => {
  this.$set(review, 'isActive', false)
})

Once these properties are dynamically appended, you can proceed with altering them without employing $set

<button @click="review.isActive = true">

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

React - Unable to perform 'removeChild' on 'Node' object: The specified node cannot be removed as it is not a child of this node

I am encountering an issue in my React project with the following structure: <div className="App"> <BrowserRouter> <BasicLayout title={"test"}> <Routes> <Route path="/home&qu ...

Is there a way to update a variable in a controller using AJAX?

Is it possible to update a variable in the controller using Ajax? Controller: $basl = array(2018,11,18,0,0); $deger = 3; $baslamatarihi=Carbon::create($basl[0],$basl[1],$basl[2],$basl[3],$basl[4]); $bitistarihi = Carbon::create($basl[0],$basl[1],$basl[2] ...

What is the best way to send and utilize a variable from app.js in index.ejs?

Why are my attempts to pass variables from app.js to index.ejs not working? I am using Mongoose and EJS, and I want to display database data on my web-page instead of just using console.log(). How can I do this correctly? (app.js snippet followed by inde ...

Troubleshooting: Issue with AngularJS ng-click functionality within Modal

My Modal doesn't open when the button inside it is clicked. The ng-click function in the js file is not being called without any error messages. However, outside of the Modal, the ng-click works fine. <div class="container-fluid" ng-app="app" ng- ...

What is the best way to create a layout with two images positioned in the center?

Is it possible to align the two pictures to the center of the page horizontally using only HTML and CSS? I've tried using this code but it doesn't seem to work: #product .container { display: flex; justify-content: space-between; flex-w ...

Why are users not visible in the Node.js application?

Good day! I am a newcomer to this community and I'm hoping to get some guidance in the right direction with my inquiries. Recently, I came across some code on Github that I am trying to improve upon. The code can be found at https://github.com/voroni ...

Numeric value along with two characters following the decimal place

Imagine I have this number: 25297710.1088 My goal is to add spaces between the groups of digits and keep only two decimal places, like this: 25 297 710.10 I tried using the following code snippet: $(td).text().reverse().replace(/((?:\d{2})&bso ...

Sending Java Servlet JSON Array to HTML

I am currently engaged in a project that requires extracting data from a MySQL database and implementing pagination. My approach involves utilizing JSON AJAX and JavaScript, although I am fairly new to JSON and AJAX. After successfully retrieving the neces ...

Transferring an array from PHP to JavaScript via an Ajax response

Welcome to my first post on stackoverflow. I usually find answers from existing posts, but this time I'm facing a problem that none of the suggested solutions have been able to fix. Currently, I have a JavaScript function that makes an AJAX request t ...

Commencing CSS Animation Post Full Page Loading

I am looking for a solution using WordPress. In my specific case, I want the CSS Animations to take effect only after the page has completely loaded. For example: click here This is my HTML: <div class="svg-one"> <svg xmlns="ht ...

A step-by-step guide on implementing the bliss view engine in Express.js instead of Jade

Is there a way to utilize the bliss view engine instead of the typical jade engine in Express JS? I came across an article on Stack Overflow, but it seems to be geared towards an older version of express.js. I am using version 3.x. Specifically, I am int ...

As I iterated over the Vehicles API data, I encountered an issue while trying to access specific Vehicle information. I received an error message stating "Cannot read property 'id' of undefined

Exploring the realms of Angular, with some experience in older versions, I find myself faced with a challenge involving two APIs - "/vehicles" and "/vehicle/{id}". The process involves fetching data from "/vehicles", iterating through it to match IDs, the ...

Chinese Version of Element UI Pagination instead of English

Recently, I've integrated the impressive Element UI components into my project. However, when trying to add the pagination component using the following code: <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :cu ...

Guide on integrating Select2 with webpack

I recently acquired the select2 node module with this command: npm install select2 After adding it to my app.js: require('select2')($); Although no errors appear when I use webpack, upon opening the application, I encounter: Uncaught TypeEr ...

Add the $scope ng-click event to a previously hidden element once it becomes visible

If you need a clearer explanation, feel free to ask. I have incorporated a global search function into the header of my website. I am looking to show a separate input box for mobile search that utilizes the same ng-click event, but the input field remains ...

When submitting a form with a contenteditable div and text box using JQuery, the POST request is not

I developed a website that allows users to edit and format text, then save it onto the server. Utilizing jquery to send the data to a PHP page for saving, I encountered an issue where the site fails to send the file name along with the formatted text to PH ...

Discover the best method for retrieving or accessing data from an array using Angular

In my data processing task, I have two sets of information. The first set serves as the header data, providing the names of the columns to be displayed. The second set is the actual data source itself. My challenge lies in selecting only the data from th ...

What is the most effective way to implement a single modal throughout my web application without having to duplicate HTML code on each page?

After realizing I was repetitively adding the same div to every page for a modal dialog, I decided to place a single div in the site.master page and call it when needed. This method worked fine until I began implementing ajax with partial page updates. H ...

How about setting variables in Vue router as an alternative to controlling component visibility?

Utilizing URL parameters to set variables in my Vue application is a goal of mine. While I initially thought vue-router would be the perfect solution, it appears to be more catered towards handling the showing and hiding of components within a router-view ...

AngularJS factory problem

I attempted to utilize Angular JS by using a factory for my data structure... However, it seems to be malfunctioning. I simply added some basic json data to be returned by the factory, and as a result, the script stopped working. This is a fundamental ex ...