Strange Behavior in Vue Component - Loader Animation Fails to Display During Data Processing

I'm facing an issue where the loader for long data processing doesn't show up until after the processing is complete. I have a large object containing thousands of key-value items that I want to make filterable, which works but takes a few seconds to load. I am using VueJS 2.

To indicate to users to "please wait" while the processing is ongoing, I am using the isworking value. I have a span with v-if="isworking", initially set to false.

When setting the this.isworking property in the first line of code, instead of displaying the "please wait" message immediately, the function hangs for a few seconds before changing the isworking property to true once the processing is done - I confirmed this by removing the last isworking=false. I can't seem to figure out why it delays updating the property to true until after the lengthy processing finishes.

The window.deaccent method is used to replace accented characters in strings with basic ANSI characters.

In the template, I have a simple form structure:

<form @submit="searchmath">
 <span v-if="working">please wait</span>
 <input v-model=...>
 <div v-for="(item,index) in searchmatchitems"> ... </div>
</form>

The method in the component looks like this:

    searchmatch: function($event){
      this.isworking = true;
      this.$forceUpdate(); // also tried this, but no luck
      $event.stopPropagation();$event.preventDefault();
      try{
        var searchid = window.deaccent(this.search_string.toLowerCase());
        var searchobj = this.cdata;
        let result = Object.keys(this.cdata).filter(function(el,i,c){ 
          var elk = window.deaccent(el).toLowerCase();
          var elv = window.deaccent(searchobj[el]);
          return elk.indexOf(searchid) > -1 || elv.indexOf(searchid) > -1;
        }, searchid);
        this.searchmatchitems = result;
        this.isworking = false;
      } catch(e){ console.log(e); this.isworking = false; return [];}
    }

I've also attempted moving the event.preventDefault() to the end just to ensure it's not causing any issues, but unfortunately, it didn't help.

The cdata object is a simple key-value structure with approximately 4000 items like this:

data: {
  cdata: {
             "lorem": "aa",
             "ipsum": "bc",
             "dolor": "de",
             ....
         },
  isworking: false,
    ....
}

Answer №1

There are two main issues to address in this situation.

First, there is inconsistency between the use of working in the template and isworking in the data and code.

Secondly, the method in question does not involve any asynchronous code execution, which results in setting isworking to true, performing work, and then setting it back to false without allowing the template to refresh. This causes the UI to freeze until the method completes.

To resolve this problem, consider making an async call to a network endpoint and setting isworking to false within the callback function. This will ensure that the expected behavior is achieved.

If you anticipate long-running code and want to prevent the UI from freezing, utilizing a web worker thread may be necessary.

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

Leveraging unplugin-vue-components within a monorepository

Situation In my monorepo (Using Turborepo), I have multiple Vue3 apps and packages. Each app is built with Vite and uses unplugin-vue-components for importing components automatically from the specific app. Additionally, I need to include components from ...

When incorporating vue-router with vuejs3, an error message may appear: Vue warn - Component does not have a template or render function

I'm attempting to create a basic router using vue-router on vuejs3, and I encountered this warning upon clicking the link for the first time (but not subsequent clicks): vue@next:1571 [Vue warn]: Component is missing template or render function. The ...

What is the significance of the source element in Vue3's audio element?

Playing mp3 files works in Vue 2, but not in Vue3. <template> <audio src="../file_example_MP3_700KB.mp3" controls ></audio> </template> In Vue3, the code needs to be modified as follows: <template> <audi ...

Impress.js functions properly within a Vue component, however, the 'visibility:hidden;' style is not consistently overridden in standard HTML

Recently purchased a commercial license and attempting to follow the documentation, but encountering issues with getting the animation to function My setup includes Laravel 5.4 and Vue 2 <head> <link rel="stylesheet" href="/css/animate.min.cs ...

The component has been successfully registered, but it appears to be unused according to the Vue guidelines

Recently, I finished creating my component called EmployeeTable.vue. After completion, I exported it as: <script> export default { name: "employee-table" }; </script> However, when I tried to import it into App.vue using the following code: ...

Implementing a toggle function in Vue.js to add or remove a class from the body element when a

I'd like to add a toggleable class to either the body element or the root element("#app") when the button inside the header component is clicked. Header.vue : <template lang="html"> <header> <button class="navbar-toggler navbar-tog ...

Insufficient column height when using CSS flex-grow property

I am facing an issue with my 3 flex columns that have varying text lengths, resulting in uneven heights. I have tried using flex-grow: 1; to make them match in height, but it does not seem to be working as intended. Can someone please help me identify what ...

Ways to modify a global variable across all components

I am looking to implement an automatic system that changes the page title according to the view I am on. Initially, I considered using a global variable in my main.js file: // MAIN.JS FILE const App = createApp(AppVue).use(router) App.config.globalProper ...

conditional rendering in a cascading sheet

Can the v-if directive be used within a style block in this manner? <style lang="scss" v-if="pinkTheme"> .ac-textfield { background: hotpink; } </style> I attempted to implement this, but unfortunately it did not work (the v-if directive ...

Designing an architecture with Rails API and VueJS server layout

Currently, I am working on a Rails API only app for the backend, along with two VueJS apps serving as the front ends. These Vuejs apps are making multiple calls to the Rails API. As I contemplate deploying my project to Digital Ocean, one question arises: ...

Performing Vue CLI Global Upgrade

Struggling to create a new Vue.js project using the Vue CLI service, I encountered an error. With @vue/cli-service 3.0.0-beta.7 installed (confirmed by running vue -V), attempting to start a new project triggered the following error: ...

[VUE Alert]: Rendering Error - "Sorry, there is a type error: object is currently undefined."

<script> const app = new Vue({ el: '#app', data:{ results:{} }, mounted() { axios.get('{{ route('request.data') }}').th ...

Utilizing vuelidate in Vue 3: Overcoming Decorators Challenge in Composition API (`<script setup>`)

Currently working on upgrading from vue 2 to vue 3 and encountering an error with the @Component decorator: "Decorators are not valid here.ts(1206) (alias) Component(options: Vue.ComponentOptionsBase<Vue, any, any, any, any, any, any, any, string, ...

Placing a component within a div element using Vue

I recently integrated a Vue module to make objects draggable and resizable on my project. In my design, I envisioned creating movable and sizable boxes that contain dropdown menus, input fields, and radio buttons. As a starting point, I have included a sim ...

Implementing dynamic component swapping in Vue 3 using components from another component

I currently have a display component called app-display, which contains a dynamic component inside (by default, it is set to app-empty): app.component('appDisplay', { template: `<component :is="currentComponent"></c ...

Retrieve Vuejs template by either module or ID

In my .Vue file, I have defined a template along with its subcomponents with the intention of allowing customers to override this template without needing to modify any javascript code. If there exists an element with id="search-result", then that element ...

Implement a formatter function to manipulate the JSON data retrieved from a REST API within the BootstrapVue framework

My bootstrap-vue vue.js v2.6 app is receiving JSON data from a REST API. The data structure looks like this: { "fields": [ { "key": "name", "label": "name", & ...

Issues with running Vue commands have been reported in Git-Bash, however they seem to work fine in

Whenever I check the version of Vue in my Terminus bash by running vue --version, here's the output I receive: $ vue -v /bin/sh: /Users/kirkb/AppData/Local/Yarn/bin/../Data/global/node_modules/.bin/vue: No such file or directory In PowerShell, when I ...

Ways to ensure that v-model does not become "true" or "false" in an input checkbox using Vue

I am currently working on a filter popup that includes a list of checkboxes. By default, some items should be selected and others not selected. I have connected these checkboxes to an object array using v-model. My issue is that when I deselect and select ...

Issue with Vue.js: routes are not found upon page refresh

Here is a basic vue-routing example: const Foo = { template: '<div>foo</div>' } const Bar = { template: '<div>bar</div>' } const routes = [ { path: '/foo', component: Foo }, { path: '/ba ...