Changing the store by triggering from a child component does not result in the store being updated

HTML Parent File

<div v-if="!refresh" class="c-focus-mode">
  <div class="c-focus-mode__container">
    <div class="c-focus-mode__container-left">
      <slide-card
        focus
        hideBottom
        :slideID="slideID"
        :index="orderIndex"
        :totalCount="5"
        :totalDuration="10"
        :isMobile="false"
      >
// Not working (1)
      <template v-slot:prevSlideBtnLeft>
        <app-button
          v-if="!prevInvalid"
          type="circular"
          class="c-slide-card-preview__btn c-slide-card-preview__btn--prev"
          @click="goToSlide(-1)"
          ><app-icon name="chevron-left"></app-icon>
        </app-button>
      </template>
      <template v-slot:nextSlideBtnRight>
        <app-button
          v-if="!nextInvalid"
          type="circular"
          class="c-slide-card-preview__btn c-slide-card-preview__btn--next"
          @click="goToSlide(1)"
          ><app-icon name="chevron-right"></app-icon>
        </app-button>
      </template>
      </slide-card>
// Working below (2)
      <div
        class="c-focus-mode__container-left-navigation"
        :class="{['c-focus-mode__container-left-navigation--no-prev']: prevInvalid}"
      >
      <app-button
        v-if="!prevInvalid"
        type="circular"
        class="c-slide-card-preview__btn c-slide-card-preview__btn--prev"
        @click="goToSlide(-1)"
        ><app-icon name="chevron-left"></app-icon>
      </app-button>
      <app-button
        v-if="!nextInvalid"
        type="circular"
        class="c-slide-card-preview__btn c-slide-card-preview__btn--next"
        @click="goToSlide(1)"
        ><app-icon name="chevron-right"></app-icon>
      </app-button>
      </div>
    </div>
    <slide-card
      focus
      hideTop
      :slideID="slideID"
      :index="orderIndex"
      :totalCount="5"
      :totalDuration="10"
      :isMobile="false"
    >
    </slide-card>
  </div>
</div>

Functions triggered by the app-button components

  public goToSlide(increment) {
    const newIndex = this.orderIndex + increment;
    if (newIndex < 0 || newIndex >= this.deckSlides.length) {
      return;
    }
    this.setActiveSlide(newIndex);
  }

  private setActiveSlide(index) {
    this.slideIndex = index + 1;
    const { deckID, ID: slideID } = this.deckSlides[index];

    if (deckID && slideID) {
      this.$store.dispatch('deck/setActiveSlides', { deckID, slideID });
      history.replaceState({}, null, `/d/${this.deckID}/edit/focus/${index + 1}`);
    }
  }

When goToSlide is called from the buttons at "Works below (2)", the store is updated successfully. However, when the same function is called from the buttons at "Not working (1)", the store is not updated. This issue also occurs when trying to trigger the goToSlide function in the parent component from the child component slide-card.

Answer №1

It seems like the issue you are facing is due to attempting to invoke a method from within the slot, where by default the slot does not have access to the data and methods defined in the parent component.

A possible solution would be to provide access to the method for the slot using 'scoped slots'. You can refer to the documentation on scoped slots through this link:

Vue Scoped Slots

You may also find the following answer helpful:

Calling a Method from a Component's Slot

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

Ensuring the accuracy of a birthdate using JavaScript

Can you help me with validating a birthdate using JavaScript function? This is the JavaScript function: function validateDate() { var inputDate = document.getElementById("d").value; var month = inputDate.substring(0, 2) - 1; var date = i ...

Chrome app experiencing issues with Angular UI router

I am in the process of developing a chrome application using angularjs along with angular-ui-router. app.js var bar = angular.module('bar', [ 'ui.router', ]) .config(['$stateProvider', '$locationProvider', ...

The user type is not yet loaded from Firestore when the view is rendered

I am currently in the process of developing an Ionic - Angular application that allows hospital patients to submit requests to nursing staff, who can then view the assigned requests based on the patient's room. Nurses have access to all requests, whil ...

Can JavaScript be used to continuously monitor a window variable object in real-time?

Is there a way to dynamically control a variable in JavaScript? I'm currently working on a code that needs to display a button when it reaches the last signature of an automatic request process. The code for activating/deactivating the button is show ...

Error message: "The variable _ is not defined when trying to use angular-google-maps library"

I'm encountering an issue where I'm receiving a ReferenceError: _ is not defined while using the angular-google-maps I'm puzzled as to why this error is occurring, as I believe I am following the instructions provided on the website. I hav ...

Having trouble with Vue re-rendering when adding an object to an array of arrays?

I'm encountering a strange issue where I can't successfully add an object to an array of arrays. Although I can view the value of groups, modifying it seems to be problematic. Below is the function that should push to the groups array based on ...

The order of key:value pairs in documents created by Mongoose does not always align with the schema

Having trouble understanding how the Mongo .create and .findAndUpdate operations work. My mongoose version is 5.4.2X and I have a model with a schema containing multiple key:value pairs in a specific order (using 1. 2. 3. etc to indicate the correct order) ...

Solution: "The Mongoose Model.find method consistently provides an empty result set."

Currently, I am utilizing mongoose in combination with next.js to interact with an existing collection. Despite the collection not being empty, the mongoose model.find() function consistently returns an empty array. Below is my model code: const mongoose ...

Exploring the Pros and Cons of Using a Broken Link Checker in Javascript

I've been working on a series of blog posts that feature tables containing information about 50 YouTube videos. Each row includes a thumbnail, video title, publishing date, and a link to my MediaWiki for additional details (e.g. "http://example.com/1x ...

What is the correct way to embed a script tag within a block of JavaScript code?

I'm currently working on a JavaScript code that needs to be executed inside an iframe. The goal is to insert a script tag within the JavaScript code and then append it to the parent body of the iframe. Below is the snippet of my code attempting to ach ...

Enable synchronized scrolling and a fixed/sticky header with splitpanes feature

Sandbox.vue <template> <v-container fluid> <Splitpanes class="default-theme" style="height: 400px" > <Pane v-for="i in 2" :key="i" > & ...

Attempting to configure Kafka consumer and producer components

Trying to establish a basic producer-consumer flow with Kafka, utilizing node-rdkafka Operating in debug: 'all' mode, the logs display: Producer: test [0]: MessageSet with 1 message(s) delivered Consumer: Fetch topic test [0] at offset 38 (v2) ...

What separates the functions `useRef` and `createRef` from each other?

As I was delving into the hooks documentation, I came across useRef. Upon examining their example… function TextInputWithFocusButton() { const inputEl = useRef(null); const onButtonClick = () => { // `current` refers to the mounted text inpu ...

Create a virtual camera in Three.js that produces an optically-inverted (mirror-image) result

In my Three.js project, I have a scene with various objects and text that are viewed through a single camera named camera_A. The output of this camera goes to a viewport_A on one part of my webpage. Now, I want to add a second camera (camera_B) that will ...

Steps for sending a form with jQuery's submit method1. Start

Below is the jquery code that I have written: $(document).ready(function () { $('.bar_dropdown').on('change', function() { console.log("dropdown changed") $('#bar_graph_form_id').submit(function(e ...

The delete function is not functioning

I need help with implementing a custom Angular directive that includes a delete button to remove itself. When I click the button removeMe, it is not deleting an item from the array. Any suggestions on what might be causing this issue? HTML: <button t ...

Refresh the view in AngularJS following a successful $http.get request

I have a <ul> in my HTML view that is populated based on a $http.get request. HTML: <div id="recentlyReleased" ng-controller="sampleRecordController as recCtrl"> <h1>Sample Records</h1> <ul> <li class= ...

Regular expressions: Capturing characters that come after and before a designated symbol

Is there a way to extract all letters, both before and after an underline "_", in JavaScript using Regex while excluding specific words like "pi", "\Delta" and "\Sigma"? How can this be achieved in Regex JS? /\b([^e|_|\d|\W])&bso ...

Transform jQuery UI Slider input into clickable links

Is there a way to turn the jQuery slider values into clickable links? For example, if the slider is at 1920, can it redirect the user to another page? I've included the code in a fiddle: http://jsfiddle.net/up6Bx/ Any assistance would be greatly app ...

Establishing a universal function within Ionic AngularJS

I have the following code snippet, where I have added a reset function that I want to be able to use from ng-click in any part of any template. angular.module('starter', ['ionic', 'starter.controllers', 'starter.services ...