Vue JS Spinner that can be used for multiple components within a single page in the same Vue application

Welcome to my Vue.js app. I am looking to add separate spinners inside each component's body instead of loading for the entire app. If there are any other spinner plugins available, feel free to suggest them.

CDN links for Vue.js

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2056554560120e15">[email protected]</a>/dist/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-loading-overlay@3"></script>
<link href="https://cdn.jsdelivr.net/npm/vue-loading-overlay@3/dist/vue-loading.css" rel="stylesheet">

HTML Code

 <main id="app">
        <div class="clearfix" style="width:200px;height: 200px; border:1px solid; border-color: #ff0000">
            cgg <comp1-loading><loading :active.sync="visible" :can-cancel="true"></loading></comp1-loading>gg
        </div>

        <div class="clearfix" style="width:200px;height: 200px; border:1px solid; border-color: #ff0000">
        cgg <comp1-loading><loading :active.sync="visible" :can-cancel="true"></loading></comp1-loading>gg

        </div>

        <h1>Vue Loading Overlay demo</h1>
        <button @click.prevent="open()">Programmatic show</button>
        <button @click.prevent="show()">Component show, cancellable</button>
        <hr>
    </main>

The following shows the Vue.js App Component. There are two components within the same app, and I want to load each one separately.

<script>
    Vue.use(VueLoading);
    
    Vue.component('comp1-loading', {
      data: function () {
        return {
          loading: true,
          dataList:[],
          something: false,
          message:"fgd"
        }
      },
      mounted(){
        this.fetchData();
        let loader = this.$loading.show({
                    loader: 'dots'
                });
                    setTimeout(() => loader.hide(), 3 * 1000)

      },
      methods:{
        fetchData:function(){
            var vm=this;
            vm.message="Hai this is spinner div";
        },
        serverDateFormat: function(date) {
        }
      },
      template: `<div >{{message}}</div>`
    })

    Vue.component('comp2-loading', {
      data: function () {
        return {
          loading: true,
          dataList:[],
          something: false,
          message:"fgd"
        }
      },
      mounted(){
        this.fetchData();
        let loader = this.$loading.show({
                    loader: 'dots'
                });
                    setTimeout(() => loader.hide(), 2 * 1000)

      },
      methods:{
        fetchData:function(){
            var vm=this;
            vm.message="Hai cthis is spinner div";
        },
        serverDateFormat: function(date) {
        }
      },
      template: `<div >{{message}}</div>`
    })

    Vue.use(VueLoading);
    
    let app = new Vue({
        el: '#app',
        data() {
            return {
                visible: true
            }
        },
        components: {
            Loading: VueLoading
        },
        methods: {
            open() {
                console.log('open was clicked, will auto hide');
                let loader = this.$loading.show({
                    loader: 'dots'
                });
                setTimeout(() => loader.hide(), 3 * 1000)
            },
            show() {
                console.log('show was clicked, click to hide');
                // do AJAX here
                this.visible = true
            }
        }
    });

    </script>

Answer №1

If you're looking for a solution to manage multiple loading states in Vue, consider checking out vue-wait. This library works seamlessly with Vue and can also be optionally integrated with Vuex.

vue-wait simplifies the management of various loading states on a page without any conflicts. It operates on a basic concept of handling an array (or optional Vuex store) containing multiple loading states. The included loader component automatically detects registered loaders and displays a loading state accordingly.

You may also find their informative Medium post about it interesting.

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

Revising Vue for lazy loading upgrades

Facing a challenge in optimizing the lazy loading of components in my application. I aim to display a loading spinner while any component is being loaded, as well as handling errors with the same approach, resulting in redundant code. export default c ...

Is it possible for Node.js to not automatically restart the server when modifying .js files?

Right now I have node-supervisor set up to detect changes in .js files, and while it works well, I've realized that it restarts the server every time a js file is saved. Is there a way to save a server-side .js file without triggering a server restart ...

When a React component initializes state using props, the state does not automatically update when the component receives new props

I am utilizing Next.js with a component that is skeleton-loaded initially as the website awaits data (using tanstack react query): if (isPending) { return <MyComponent name={"Loading...."} hobbies={[]} /> } return & ...

Utilizing JavaScript to Parse Datasnap Output

I am currently working on a project in Delphi where I need to display a list of data in a listbox. However, I am struggling to understand how everything comes together. Luckily, I found a helpful answer that provided me with a solution using a dataset def ...

Utilizing Angular and Express for routing with the seamless integration of html5mode

I'm facing an issue with making angular and express routing work together with html5mode enabled. When I change state from '/' to my admins state, everything works fine until I refresh the page. Then I only get a json result of admins but my ...

Having trouble iterating over a JSON object in a Node.js application

I am struggling with looping through a JSON object using NODEJS. Specifically, I am attempting to access the values like var value = [table][row][0][STATE] or [table][row][1][STATE] or [table][row][2][STATE], but I keep encountering an error saying STATE ...

Issue with `npm run watch` failing to compile when the data source is turned

Currently, I am faced with a challenge while working on Laravel and utilizing various node packages for development. The issue at hand is my limited internet connectivity. Every time I attempt to execute npm run watch, it refuses to initiate unless I am c ...

Using JavaScript to modify the text of a label seems to be a challenging

UPDATE: After carefully reviewing my code once again, I have identified the issue. The problem lies in the positioning of a certain function (unfortunately not included here), but rest assured that I have rectified it! Allow me to provide a brief summary ...

Unexpected behavior in React-Native when filtering array objects

I am currently working on a react-native project where I am dealing with an array object that is being fetched from the backend. Here is a snippet of the Array. { "2010":[ { "id":1243, "eventName": ...

Unlocking the Power of Large Numbers in Angular with Webpack

Error: [$injector:unpr] Unknown provider: BigNumberProvider Recently, I embarked on a new project using Webpack + Angular.JS and encountered an issue while trying to incorporate Bignumber.js. Here's a snippet of my Webpack configuration: resolv ...

Cease the continuous playback on the chromeless player of YouTube

When I play videos on my website, I use the function provided below. I want the videos to start automatically, but I don't want them to repeat once they end. I tried changing the loop parameter to loop=0, but unfortunately, it didn't work. Do I n ...

Can you provide some guidance on passing a PHP object or array to a Vue prop without using Blade?

This is not Blade, instead it's a regular PHP file containing the inline-template of my Vue component. Therefore, I do not have access to the typical Laravel/Blade {!! !!} shorthand. I am currently in the process of migrating an old Codeigniter proje ...

so many alerts to choose from aside from jsx/react

After some consideration, I believed I had devised an effective way to notify users when the array is empty and there's nothing to display. However, upon implementation, I realized my technique only works onFocus or page reload because I placed the fu ...

Combining Bootstrap Vue: utilizing class names alongside HTML tags

(Bootstrap-Vue 2.0, Vue.js 2.5) Is it possible to combine traditional CSS Bootstrap 4 classes with Bootstrap-Vue? For example, can I use the following code snippet: <section id="introduction"> <b-container class="h-100"> & ...

Tips for retrieving multiple values or an array from an AJAX request?

Is there a correct way to pass multiple sets (strings) of data back after executing an ajax call in php? I understand that echo is typically used to send a single string of data back, but what if I need to send multiple strings? And how should I handle th ...

What could be causing the NoScript tag to malfunction across different web browsers?

I've incorporated the NoScript tag into my JSP pages in the head section. To avoid conflicts with tiles, I made sure not to include multiple NoScript tags. However, I am experiencing issues in Internet Explorer where it doesn't seem to be working ...

Performing an asynchronous AJAX request to a PHP script to compute the total sum and retrieve

I have created an HTML table and utilized AJAX to send a POST request with two arrays. The first array is named rowValues and contains three arrays, one for each row. The second array is called columnValues and also has three arrays, one for each column. ...

What steps can be taken to resolve the error message "t.onSubmit is not a function" that occurs upon form submission?

Upon submitting a form, it should trigger the onSubmit method function. However, an error is being returned instead: TypeError: "t.onSubmit is not a function". I've attempted to address this issue by researching similar problems and solutions provide ...

Instructions for obtaining and assigning a reference to a recently cached associated object within the Apollo client InMemoryCache

My data consists of a set of interconnected items like this: book { id ... related_entity { id ... } } Once Apollo caches it, there are two separate cache objects. The related_entity field on book points to an EntityNode object. Everything ...

Unlocking the power of v-model within your custom component

Is there a way to tap into the initial value of a v-model on a vue custom component? I know we can bind the value to a parent component using this.$emit(input, e.target.value). I've already created an upload-button component that uploads files when a ...