Ways to access the element in Vue.js using ref for shepherd tutorial

I am currently utilizing Shepherd as my tour guide in Vue. You can find the documentation here:

Below is the component I have created for it:

<template>
  <div></div>
</template>

<script>
export default {
  name: 'Guide',

  props: {
    element: {
      type: Object,
      required: true,
    },

    title: {
      type: String,
    },

    text: {
      type: String,
      required: true,
    },

    position: {
      type: String,
      required: true,
    },
  },

  mounted() {
    this.$nextTick(() => {
      const tour = this.$shepherd({
        defaultStepOptions: {
          cancelIcon: {
            enabled: true,
          },
          classes: 'class-1 class-2',
          scrollTo: { behavior: 'smooth', block: 'center' },
        },
      });

      tour.addStep({
        attachTo: { element: this.element, on: this.position },
        title: this.title,
        text: this.text,
        buttons: [
          {
            action() {
              return this.back();
            },
            classes: 'shepherd-button-secondary',
            text: 'Back',
          },
          {
            action() {
              return this.next();
            },
            text: 'Next',
          },
        ],
        id: 'creating',
      });

      tour.start();
    });
  },
};
</script>

<style lang="scss">
@import '~shepherd.js/dist/css/shepherd.css';
</style>

The issue I'm facing with my code is that in the parent component, I am unable to retrieve the element. I've attempted using ref in Vue to achieve this. Specifically, I need to access the element of this button:

<button ref="button">
              Click
            </button>
beforeMount() {
    this.element = this.$refs.button;
  },

However, the element is consistently returning null. How can I successfully obtain the element of the button?

Answer №1

https://v2.vuejs.org/v2/api/#mounted

To access the DOM element, it is recommended to place the code within the mounted hook.

mounted() {
    this.element = this.$refs.button;
}

Please note that the mounted hook does not ensure that all child components have been mounted as well. If you need to wait until the entire view has finished rendering, you can utilize vm.$nextTick inside the mounted hook.

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

What could be the reason behind the error message "Java heap space exception in Eclipse" appearing while trying to use JavaScript autocomplete?

Whenever I attempt to utilize a JavaScript template on Eclipse, the program always freezes, displaying an error message stating: "Unhandled event loop exception Java heap space." To troubleshoot this issue, I initiated a top command in Ubuntu for both the ...

Implement the morgan express middleware from exports

For my logging needs in an Express application, I have decided to utilize the morgan package. Initially, I was able to integrate morgan by using it directly in my server.js file like so: app.use(morgan('tiny')), which worked perfectly. However, I ...

Automatically sending all the input variables as an array using AJAX in PHP

Seeking a way to streamline the process of updating an AJAX call when changes are made to a form. For example: FORM: <form> <label>RED</label> <input type="text" id="RED"><br> <label>BLUE</label> ...

Guide on redirecting to a different page and showing a flash message in Next.js

Currently working on Reactjs and nextjs, my goal is to submit a form using axios(Api). The form submission is successful, but I would like the page to change after submitting the form (displaying Allblogs.js, similar to a redirect) along with a flash mes ...

Is there a more effective approach to designing a login screen?

I am just starting out with angular js and this login page is my first step. I have created a basic login form, but when I click on the login() button, the form() does not get submitted and it keeps showing an error message saying "invalid username and pas ...

passing JSON data using JavaScript or jQuery

I have a JSON snippet that I need to parse using JavaScript or jQuery and convert into variables: name and meetup. Can you help me with this? Below is the JSON code: { "MYID": 1, "module": [ { "name": "Manchester", ...

Guide on sending an AJAX request to a server

I'm currently working on implementing AJAX and have encountered a roadblock. Specifically, I need assistance with sending a request to the server when a user clicks on a specific image, expecting the server to return that image. While I know how the s ...

The window.addEventListener function is failing to work properly on mobile devices

Hey there! I am facing an issue in my JS code. I wrote this code because I want the menu to close when a visitor clicks on another div (not the menu) if it is open. The code seems to be working fine in developer tools on Chrome or Firefox, but it's no ...

Incorporate a `fresh Audio` element into my redux repository

I'm attempting to include a new Audio element in my redux store. This is how my reducer appears: export const songsReducer = (state = {}, action) => { switch (action.type) { case "PLAY_SONG": return { ...state ...

"Implementing drag and drop functionality in Vue.js without the need for a

Exploring html 5 drag and drop functionality using vue js has been a challenging experience for me. After following the w3schools tutorial on drag and drop, I managed to get it working in a basic html file but faced difficulties when implementing it in my ...

What is the best way to refresh the parent div of a component in vue.js?

Have you ever wondered if it's possible to achieve this code snippet: <div v-for="tile in container" v-bind:class="proper-class"> <tile :tile='tile' @update="update-class"></tile> </div> The goal is to change th ...

"Looking to display an image object retrieved from AWS S3 using a signed URL in Vue.js? Here's how you

<div v-for="(data, key) in imgURL" :key="key"> <img :src= "getLink(data)" /> </div> imgURL here is an array that contains file names. methods: { async getLink(url){ let response = await PostsService.downloadURL({ i ...

Trouble with AJAX BitMovin: unable to process GET request

I've been struggling to find a solution to my problem as I cannot pinpoint what exactly to search for. My current issue involves using AJAX for a PHP Get request, causing it to submit and navigate away from the page. The BitMovin library appears to b ...

Utilize Vue.js to transform a two-dimensional associative array into an Object

I have a multi-dimensional array called maplist, structured as shown below: https://i.sstatic.net/CYZA9.png Now, I want to convert it into an object like the one displayed in the following image: https://i.sstatic.net/wXqyl.png The resulting object shou ...

Tips for effectively managing asynchronous tasks

I keep getting different numbers every time my code runs. Can you tell me if I'm doing this the right way? Here's the code: export class GetPlanetsService { url='https://swapi.co/api/planets/?page='; planets:Planet[]=[]; headers: ...

What is the method to capture the change event in a datalist element?

Currently working with a datalist, I am in need of detecting when a user selects an option from the drop-down list. Although a similar question has been brought up, my requirement is for the event to only trigger when the user actually makes a selection fr ...

Using the <object> tag in AngularJS partials for improved functionality

Trying to incorporate the <\object> tag or <\iframe> within a partial HTML file and then include that HTML in another page using ng-include. Here is my attempt: <div class="container"> <!-- This section doesn't displ ...

A method in JQuery to replace an input with a select element is by using the replace

I have multiple input fields with pre-existing values. My goal is to replace these inputs with select dropdowns that have the same options while retaining the original values. Here is the current HTML structure: <span id="span1"> <input type=" ...

Nuxt rendering is causing server side state changes to vanish

I'm grappling with a perplexing issue that has me stumped. Allow me to explain the situation in detail. Within my Nuxt project, I have implemented a modular store. There is a global middleware that functions exclusively on the server side (i.e., upo ...

Adjustable number of columns in CSS grid

As of now, my webpage consists of a grid of articles with an absolutely positioned footer. My goal is to make the article grid have a variable number of columns based on the window height. I've considered calculating the distance between the filter ba ...