Have Vue props been set to null?

Currently, I have a component within my vue.js application that looks like this:

export default {
    props: ['forums'],

    methods: {
        increment(forum, index) {
            ForumService.increment(forum)
                .then(() => {
                    this.forums.splice(index -1, 2, this.forums[index], this.forums[index -1]);
                });
        },
    }
}

However, when attempting to increment using:

<i class="material-icons" @click="increment(forum)">&#xE316;</i>

The prop forums ends up becoming null (which is visible in the vue devtools). Is there a solution to resolve this issue?

Answer №1

To ensure proper functionality, it is important to create a local copy of your prop within the created hook. By doing so, you can then utilize this local variable instead:

export default {
 props: ['forums'],
  created() {
    this.localForums = this.forums;  // Copy prop to local variable
  },
  methods: {
    increment(forum, index) {
      ForumService.increment(forum)
        .then(() => {
          this.localForums.splice(index - 1, 2, this.localForums[index], this.localForums[index - 1]);
        });
    },
  },
  data() {
    return {
      localForums: []
    }
  }
}

You can now proceed to invoke your method in the following manner:

<i class="material-icons" @click="increment(forum, 1)">&#xE316;</i>

A visual demonstration of how this operates has been established via a JSFiddle. Please note that the specific details of ForumService.increment(forum) and forum are unknown, hence a mock scenario has been presented with a returned promise to illustrate the absence of any scope-related complications:

https://jsfiddle.net/wu6ad78m/

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

Maintain the previous state in AngularJS using ui-router

My goal is to preserve the current state of the view, not just the URL and parameters. I am looking to save the entire view along with its scopes. Specifically, I am working on creating a search system similar to Facebook's, but with a unique twist. I ...

Creating a custom login directive in Angular 2 and utilizing location.createComponent for dynamic

Incorporating a login system into my Angular app has been a priority for me lately. I came across a helpful resource here that outlines the process. However, I encountered an issue with the custom RouterOutlet directive as shown below: import { ElementRef ...

Using Twig path to pass Ajax URL parameter

I'm facing an issue in my Twig view when passing parameters in the AJAX URL path. Since Twig is executed before JavaScript, it doesn't recognize the input value passed as a parameter. Is there a way to solve this without passing the param in data ...

Storing/Caching API Requests with NUXT.JS

I am in the process of developing a NUXT.JS application that retrieves JSON data from an API called Storyblok. I would appreciate some advice or suggestions on how to efficiently store/cache the API response to avoid making multiple requests when navigatin ...

A Step-by-Step Guide to Launching PDF Blob from AJAX Response in a Fresh Chrome Tab

I'm sending a POST request to a server, and in response, I receive a PDF file that is returned as a blob. To handle this blob, I am using the "handle-as='blob'" attribute of iron-ajax (a Polymer element), just to cover all bases. Now, my g ...

Is there a way to retrieve the value of elements that are deeply nested within multiple objects and arrays?

When making an API call to retrieve data from the Google Distance Matrix API, I store that information in my Redux store within a React application. The returned data object is structured as follows: Object { "destination_addresses": Array [ "21 Fo ...

What is the proper method for delivering Javascript code with rendered HTTP to a client?

During the development process, I made a decision to switch to server-side rendering in order to have better control and take advantage of other benefits. My web application relies heavily on AJAX with no url redirecting, creating a website that dynamicall ...

Submitting form by clicking a link on the page

To submit a POST request with "amount=1" without displaying it in the URL, I need the site to send this request when any link on the site is clicked. This JavaScript code achieves that with a GET request: window.onload = function () { document.body.oncli ...

Tips for leveraging a button to trigger server-side actions

Being a novice in web development, I'm currently working on a straightforward website that enables users to download files from the server side. These files are not pre-created; instead, there will be a button on the HTML page. When a user clicks this ...

Send information using AJAX's POST method

Can an image file be uploaded using the jQuery ajax post method? Will it work if the file data is simply placed in the POST request's 'data' parameter? I am working with the django framework and this is my initial attempt: $('#edit_us ...

Looping through a set of API calls using JavaScript Web API

Currently, I am in the process of developing an application using angularjs and ionic. Within this app, I have an array containing IDs, and my objective is to retrieve their corresponding names. To achieve this, I attempted the following code snippet: var ...

Steps for populating an ng-table with data retrieved from a REST web service that returns a JSON format

I am currently facing an issue while trying to load my table from a JSON response that I receive from REST web services in SpringMVC. The error message I received indicates that my REST method does not support the GET request. The URL mapped in my control ...

Preserving the button's state when clicked

Here is my code snippet: <blink> const [thisButtomSelected, setThisButtomSelected] = useState(false); var thisButton = []; const onAttributeClick = (e) => { thisButton[e.currentTarget.value] = { thisID: e.currentTarget.id, thisName: e. ...

Issues with ng-bind-html in AngularJS and JavaScript are preventing it from functioning

I am experimenting with creating a dynamic webpage that can change its contents without being limited to predefined templates (potentially offering infinite template options). Below is the code I am currently testing: <!DOCTYPE html> <html lang= ...

What are the steps to get an Icon to intersect with a v-app-component in a Vuetify project?

The locket is not overlapping with the v-app-bar. How do I make sure they intersect? <template> <v-app-bar :elevation="2" rounded> <v-row class="z-index"> <v-col cols="12" class="d-flex flex-column justify-center align ...

Guide on accessing nested objects in EJS templates

I'm attempting to extract the "info" portion from the JSON data provided below. In my code snippet, I'm using the <%= person['person_details']%> to access that specific section of the JSON. However, it only returns [Object Obje ...

When implementing 'useGlobalGuards' in NestJS, remember to exclude endpoints for enhanced security

After implementing the useGlobalGuards method in my main.ts file, all endpoints now utilize the AuthGuard. This guard triggers a 401 error if a valid auth token is not present in the request header. Previously, I used @UseGuards(AuthGuard) on individual cl ...

Guidelines for queuing method calls using Vue.js

Is there a way to invoke a method using a queue system? Imagine having a method that makes API calls and can only handle 3 calls at once. If more than 3 calls are made from a component, the remaining ones should wait until one call finishes before proceedi ...

How can I use jQuery to access the parent node in an XML document?

I have been trying to extract the top-level 'label' attribute from the XML code below using jQuery, but I haven't had any luck so far. I have already converted it into a DOM object, but the results are not what I expected. Does anyone have a ...

Load information from several folders into a dropdown menu, dynamically updating the options based on the selected folder using AJAX and JSP

As a newcomer to AJAX, I have a specific requirement to populate data in a dropdown menu. I have two dropdowns - the first one should display the names of folders stored locally on our system, each containing different kinds of files. My task is to dynami ...