Tips for accessing information from different sources within Vue 3

In my Vue 3 setup() method, I have an array [] stored as a constant. This array consists of X objects and represents the responses of a form.

My goal is to showcase each object on a single dynamic page within our internal dashboard by using v-for in the template to avoid repeating markup code. Here is the approach I have considered:

<template>
  <div>
    <div v-for="(item, index) of entries" :key="index">
      <dt class="text-sm font-medium text-gray-500">{{ item.label }}</dt>
      <dd class="mt-1 text-sm text-gray-900">{{ item.response }} // Currently empty in the template</dd>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from "vue";
import { supabase } from "../utils/supabaseClient.js";

export default {
  setup() {
    const data = ref({
      // The data object is initially empty and will be populated onMounted, but it currently shows as undefined. I also attempted beforeEnterRoute without any success.
      name: "John Example",
      company: "Example Company",
      radio: "False",
    });

    const entries = ref([
      // ... additional entries above this point
      {
        label: "Name",
        response: data.value.name, // This appears as undefined, as do the other entries where I attempted similar assignments.
      },
      {
        label: "Company Name",
        response: data.value.company, // This appears as undefined, as do the other entries where I attempted similar assignments.
      },
      {
        label: "Any Additional Information?",
        response: data.value.radio, // This appears as undefined, as do the other entries where I attempted similar assignments.
      },
    ]);

    onMounted(async () => {
      // Making an API call using the Supabase client, given that we utilize their service.
      let { data: rows, error } = await supabase.from("submissions").select("*").order("date_submitted", { ascending: false });

      if (error) {
        console.log(error);
        return;
      }

      data.value = rows;
    });

    return {
      data,
      entries,
    };
  },
};
</script>

I've also attempted using beforeRouteEnter and onBeforeMounted, but without success.

Answer №1

entries needs to be a computed property based on data in order to react to any changes in data:

import { computed } from 'vue'

export default {
  setup() {
    //...

    const entries = computed(() => [
      {
        label: "Name",
        response: data.value.name,
      },
      {
        label: "Name of the company",
        response: data.value.company,
      },
      {
        label: "Do you have something to add?",
        response: data.value.radio,
      },
    ]);

    //...
  }
}

See demo

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 is the best way to utilize the same module across multiple files within a single project?

After learning that modules are cached when required, as explained in this post, I am wondering what the most efficient way is to write clean and readable code out of the various approaches available. Situation: I have three files named A, B, and C. All ...

Resolving Deployment Issue on Azure for a Web App using Vue.js and ASP.NET with Continuous Integration and Deployment Setup

After setting up CI/CD for my Vue.js + ASP.NET web application, I aimed to host it on Azure using a Web App by following the Microsoft tutorial here However, an error arose during the publishing process while it runs smoothly on my local IIS setup. The c ...

Numerous perspectives within an angular.js application

Issue I'm currently working on creating a product list with the following initial features: Server-side pagination Server-side filtering I am facing several challenges with my current setup, but the main issue is that I can't figure out how to ...

formik does not support using the "new Date" function as an initial value

I have been trying to set the initial value of a date in my component like this, but when it renders I am encountering an error. const formik = useFormik ({ initialValues: { dob: new Date () } }) During this process, I'm facing the follow ...

The scrolling speed of my news div is currently slow, and I am looking to increase its

This is the news div with bottom to top scrolling, but it is slow to start scrolling. I want to increase the speed. The current behavior is the div appears from the y-axis of the system, but I want it to start exactly where I define. The scrolling div is ...

Troubleshooting issue: Displaying input based on selected option not functioning

<custom-select label="Target Type" v-model="targetType" name="targetType" placeholder="Select Target Type" data-test="overall-type-input" :options="targetTypeOptions ...

Ways to effectively handle diverse Angular module dependencies

Although I am still new to Angular, I have been striving to write more modular code and rely less on cramming logic into the controller. Instead, I have been utilizing independent services. However, a recurring issue I am facing is having to re-declare the ...

What is the best way to extract a JSON string from the login PHP response?

I am working on creating a basic website along with an Android application that both retrieve data from the same database. While I have no issues in dealing with Android, I am facing difficulty in handling JSON strings in HTML. How can I fetch the JSON res ...

Button fails to display as intended despite meeting conditions

I'm currently using a formData object with useState(). Whenever a field is updated in the form, I update formData like this: setFormData({...formData, [field.id]: field.value}) My goal is to have the button at the end of the form change once all req ...

Setting the Content-Type of a JavaScript file within a NodeJS application

I am facing an issue with opening a js-file on my NodeJS server, as it always specifies the .js file with a Content-Type of "text/html." My objective is to send user-input from an html form to a JavaScript file for performing calculations and later genera ...

Customize the jQuery datepicker by assigning a class to the first 17 days

How can I apply a class to only the first 17 days on a jquery datepicker calendar? I've attempted the following code, but it ends up adding the class to every day... beforeShowDay: function(date) { for (i = 0; i < 17; i++) { return [t ...

Using the factory pattern in a Node.js (Express) application

As I delved into the realm of design patterns, I found myself drawn to the factory pattern. However, as I perused through code written by others, I noticed that this particular pattern wasn't as prevalent, especially in newer stacks. Take for example ...

The Ajax POST request encounters failure, although it functions properly in the console

The ajax function below is encountering an error message with little information, simply stating "error": var form = formInfo; var url = $(formInfo).attr("action"); var data = $(formInfo).serialize(); $.ajax({ type: "post", url: ur ...

Tips for including images while drafting an article

In my current project, users are able to write articles. One feature of the editor is the ability to upload photos and include them in the article. While this functionality works well, there is an issue with how the photos are handled and stored. Each ar ...

Placing content retrieved from a MySQL database within a form displayed in a ColorBox

Having difficulty inserting text from a MySQL database into a form (textfield) inside a ColorBox. The current script is as follows: <a href="#" class="bttn sgreen quote1">Quote</a> var postQuote[<?php echo 4id; ?>]=<?php echo $f ...

What is the process for incorporating an external script into a Vue component?

Seeking assistance urgently... I am encountering an issue with a Vue component and an endpoint that provides a script containing a small menu with actions. However, once the script is loaded, the actions do not seem to function on the page and I cannot det ...

Asynchronous POST request in Chrome Extension's popup.js

Apologies for asking again, but has anyone encountered a similar issue? I have a popup.js file with a login form that sends a request to an API to retrieve an API key for user authentication. When I set the request as synchronous (async: false), everythin ...

What is the method for retrieving a child element using its ID in JavaScript?

Below is the HTML code I am working with: <div id="note"> <textarea id="textid" class="textclass">Text</textarea> </div> I am trying to retrieve the textarea element without using document.getElementById("textid"). This is what I ...

Is it possible to include pseudo element elements in the configuration of a custom theme in Material UI?

Within my file themeConfig.js, I have defined several theme variables that are utilized to style different components throughout my application. Among these variables, there is a need for implementing the -webkit scrollbar styles for certain components. Du ...

There seems to be an issue with declaring Gulp Open

Here is the code snippet for my `open.js` task: import gulp from 'gulp'; import gulpOpen from 'gulp-open'; gulp.task('open', () => { // doesn't work with `function()` either. const options = { uri: 'local ...