Laravel's Vue component is experiencing issues with loading Axios response data

Hey everyone, I hope you're all doing well! I'm facing a bit of an issue with passing data from an axios response to the data property. The data is successfully fetched from the database and displayed in the console, but when I try to display it on the component, it shows up as an empty array. Any help would be greatly appreciated. Thanks in advance!

Here's the Vue Component Script:

<script>
    import axios from 'axios';
    export default {
         data(){
                return {
                        Templates:[],
                        loading:true
                    }
        },
        async mounted() {
            await axios.post('/getTemplates')
                .then(response => this.Templates =response.data )
                .catch(error => console.log(error.message))
                console.log(this.Templates)
        }

    }

</script>

I tried calling the data prop with {{Templates}} in the component :

<article class="overflow-hidden rounded-lg shadow-lg max-w-lg">
  <a href="#">
  </a>
      {{Templates}}
  <header class="flex items-center justify-between leading-tight p-2 md:p-4">
      <h1 class="text-lg">
          <a class="no-underline text-black" href="#">
          </a>
      </h1>
      <p class="text-grey-darker text-sm">
          Text
      </p>
  </header>
  <footer class="relative flex items-center justify-between leading-none p-2 md:p-4">
      <a class="flex items-center no-underline hover:underline text-black" href="#">
          <svg width="24" height="28" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M16 12C16 14.2091 14.2091 16 12 16C9.79086 16 8 14.2091 8 12C8 9.79086 9.79086 8 12 8C14.2091 8 16 9.79086 16 12ZM14 12C14 13.1046 13.1046 14 12 14C10.8954 14 10 13.1046 10 12C10 10.8954 10.8954 10 12 10C13.1046 10 14 10.8954 14 12Z" fill="currentColor" /><path fill-rule="evenodd" clip-rule="evenodd" d="M12 3C17.5915 3 22.2898 6.82432 23.6219 12C22.2898 17.1757 17.5915 21 12 21C6.40848 21 1.71018 17.1757 0.378052 12C1.71018 6.82432 6.40848 3 12 3ZM12 19C7.52443 19 3.73132 16.0581 2.45723 12C3.73132 7.94186 7.52443 5 12 5C16.4756 5 20.2687 7.94186 21.5428 12C20.2687 16.0581 16.4756 19 12 19Z" fill="currentColor" /></svg>
      </a>
      <a class="flex no-underline hover: no-underline text-red-dark bg-blue hover:bg-blue-light text-white font-bold text-lg border-b-4 border-blue-dark hover:border-blue absolute bg-blue-500 rounded-lg px-1 py-1 transition duration-300 ease-in-out hover:bg-blue-600 right-3" href="#">
          <span>Select</span>
      </a>
  </footer>
</article> 

Blade template used:

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-xl sm:rounded-lg">

            <!-- Column -->
            <div class="my-1 px-1 w-full md:w-1/2 lg:my-4 lg:px-4 lg:w-1/3">
                <!-- Template -->
                <div id="app">
                    <buzzevent-template></buzzevent-template>
                </div>
                <!-- End Template -->
            </div>
            <!--End Column -->
            </div>
        </div>
    </div>
    <script src="{{asset('js/app.js')}}"></script>

</x-app-layout>

If more information is needed, feel free to ask.

Check out the output here:


View Result Image

Answer №1

Your ES6 syntax needs a little adjustment, make sure to include {} braces inside your .then() function like this:

<script>
    import axios from 'axios';
    export default {
         data(){
                return {
                        Templates:[],
                        loading:true
                    }
        },
        async mounted() {
            await axios.post('/getTemplates')
                .then(response => { this.Templates = response.data })
                .catch(error => { console.log(error.message) })
                console.log(this.Templates)
        }

    }

</script>

For more information on syntax, visit: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

You can also refer to this Stack Overflow question for further assistance: Issue displaying updated data in Vue component after axios POST

Answer №2

At last, it's resolved! It was discovered that the import (js/app.js) was included twice - first in the app.blade.php layout template and then again in the specific template where I called the component. And that was the issue.

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

Is there a way to automatically mount tabs without manually clicking on them?

Looking to optimize data fetches within a large tab container in React using material-ui. My goal is to have each Tab component handle its own data fetching, especially for Tabs with a greedyLoad prop that will be mounted upon the initial mounting of the T ...

Copy values of multiple input fields to clipboard on click

I have a collection of buttons in my Library, each with different text that I want to copy when clicked function copyTextToClipboard(id) { var textElement = document.getElementById(id); textElement.select(); navigator.clipboard.writeText(textElement. ...

Playing with Data in AG-Grid using Javascript

I am working on implementing data display using AG Grid with an AJAX call, but I am facing an issue where no data is being shown in the grid. Even though my AJAX call seems to be functioning correctly and returning the desired object List, the grid itsel ...

Comparison of efficiency in declaring JSON data using JSON.parse versus an object literal

In a recent video from the 2019 Chrome Dev Summit titled "Boosting App Speed with JSON.parse", it was revealed that utilizing JSON.parse with a string literal instead of an object literal can result in a significant enhancement in speed. The official Googl ...

Automatic Slideshow

I am trying to implement autoplay in my slider, but I am having trouble figuring out how to do it. The slider itself is working fine, but I know that I need to use an interval for the autoplay feature. If anyone could provide some assistance on how to ac ...

How many logical lines of code are in the Ubuntu operating system?

As I develop my web application, it is crucial for me to track the lines of code written in languages such as php, css, html, and JavaScript specific to the /var/www directory. However, when using the command line code counter tool, I find myself tempted ...

Utilizing Webpack to Import GLB Models into Three.js

Just delving into the world of Webpack for the first time and I'm encountering some difficulties when trying to add my glb model. The model itself is fine, it's been used multiple times and I placed it in the public folder. I'm puzzled by th ...

What is the reason behind the issue of an infinite loop being resolved by including additional arrow function parentheses?

I'm currently using React for my project, and I've encountered an issue with the setState hook. Below is a snippet of my code: //state and handle function const [activeStep, setActiveStep] = React.useState(0); const handleStep = (index) => ...

Effortlessly implement CSS styling in a scoped shadow element with Vue3

I am facing an issue with applying styles to an anchor element in my code. Below is a snippet of the code: <template> <custom-button> #shadow-root (open) <a href="#"></a> <other-custom></other-c ...

when input event occurs automatically upon returning to the page

<input type='text' class='binp'> $('.binp').on('input', function(){ alert('lorem'); }); After entering text into the .binp input field and navigating to another page, returning using the browser ...

Is there a way to alter the color of a single row within a column in a jtable based on a specific condition?

statusOfPayment: { title: 'Status of Payment', width: '8%', options: {'Paid': 'Paid', 'Due': 'Due'}, create: false, ...

Exploring custom JavaScript with Construct 2's generated code

How can I access the score of a game user in Construct2 Engine using external JavaScript? Is there a method to achieve this? Appreciate any guidance on this matter. ...

Determine the quantity of items that meet specific criteria

The initial state of my store is set as follows: let initialState = { items: [], itemsCount: 0, completedCount: 0 }; Whenever I dispatch an action with the type ADD_ITEM, a new item gets added to the items array while also incrementing the count in ...

Mastering Anchors in AngularStrap ScrollSpy

Is there a way to effectively utilize AngularStrap's ScrollSpy feature to link to anchors within the same document? Upon reviewing the AngularStrap documentation, I noticed that when a link is clicked, a double hash is generated in the URL. For examp ...

How can we convert a mixed-case word into one or more words with initial capital letters?

Is there a way to convert caps and spaces to their normal form? For instance, can the word coreControllerC4a be transformed into Core Controller C4a automatically when a function is triggered? ...

Obtaining the width of a scrollbar using JavaScript in the most recent version of Chrome

Looking for a way to determine the width of the scrollbar using JavaScript for CSS purposes. I attempted the following: document.documentElement.style.setProperty('--scrollbar-width', (document.offsetWidth - document.clientWidth) + "px" ...

React unable to properly render Array Map method

I am currently working on a project using ChakraUI and React to display a list of team members as cards. However, I am facing an issue where only the text "Team Members" is being displayed on the website, and not the actual mapped array of team members. Ca ...

What is the process for reinserting a list item into the nested elements?

Looking for help with manipulating a menu in HTML? I have a menu that I want to modify by removing and adding list items. While I've had success removing items, I'm struggling to properly use the add method. Here's an example of what my menu ...

"Troubleshooting the event.target[matches] issue encountered during form submission in a Meteor React project

Can anyone help me with this bug I'm facing? Here is the issue: To summarize, I have a form for creating events and I want the handleSubmit() function to manage error messages and add the event to the database if there are no errors. I previously had ...

Nuxt.js static pages with relative URLs

I am currently working on developing static pages with Nuxt.js (MPA). After executing the generate command, I noticed that all the URLs in the <nuxt-link> tag start from the root directory, specifically /. For instance, my project structure looks lik ...