What is the reason behind the inability to attach a computed property as inline style in this particular Vue 3 application?

I'm currently developing a Vue 3 audio player integration with the Napster API.

Project specifics

The player features a progress bar where I utilize the trackProgress computed property for real-time progress updates:

<div class="progress-bar">
   <span :style="{ width: trackProgress + '%' }"></span>
 </div>

...

The challenge

I have encountered an issue where the style is not properly applied to the span element within the progress-bar.

What am I overlooking?


UPDATE

Although using setInterval in the created hook functions, I seek an alternative method. Any suggestions?

this.player.addEventListener("loadedmetadata", () => {
  setInterval(() => {
    this.trackProgress =
      (this.player.currentTime / this.player.duration) * 100;
  }, 100);
});

Any improved solutions?

Answer №1

Here is a breakdown of the process used to find the solution:

  1. Create a listener for when the player's timeupdate event occurs
  2. Update the component's data whenever the timeupdate event happens
  3. Utilize a computed property called percentageProgress to determine the progress and display it in the template. (You could continue using the trackProgress property, but percentageProgress might be more clear semantically.)

To implement this solution:

data() {
  return {
    player: new Audio(),
    trackCount: 0,
    tracks: [],
    muted: false,
    isPlaying: false,
    currentTime: 0
  };
},

computed: {
  percentageProgress() {
    return (this.currentTime / this.player.duration) * 100;
  }
}

created() {
  this.player.addEventListener("timeupdate", () => {
    this.currentTime = this.player.currentTime;
  });
}

Additionally, keep in mind that computed getters must always return a value. Your current computed property does not currently return anything.

Answer №2

It is recommended to establish a data property called trackProgress and modify it within the listener that you generate in the created() hook, similar to how you handle the ended event.

Answer №3

Your approach of continuously adding event listeners within a computed property seems counterproductive.

Although these listeners fetch the desired value, they seem to be lost in the digital void as no action is taken on the returned value when the event triggers.

Instead of using a computed property, consider integrating a trackProgress property into your data() model and move the event listener addition to the mounted section to ensure it is only added once.

This listener will then update the trackProgress property in your model.

data() { return {
  player: new Audio(),
  trackCount: 0,
  tracks: [],
  muted: false,
  isPlaying: false,
  trackProgress: 0,
} }

and

mounted: function () {
  this.player.addEventListener("timeupdate", () => {
    this.trackProgress = (this.player.currentTime / this.player.duration) * 100;
  });
}

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

Having trouble with Vue/Nuxt page transitions not working correctly during the leave phase

My attempt at implementing page transitions using CSS didn't quite work, especially the leave transition. So, I turned to using JS Hooks instead, but unfortunately, the leave animations still refuse to cooperate. After meticulous troubleshooting and c ...

Angular 2 ngFor generates a collection of rows and columns forming a single large column

It seems that ngfor is generating divs one by one, resulting in a poor design where they are stacked on top of each other. I would like to achieve a layout like this: [1] [2] [3] [4] [5] [6] However, the current outcome looks like this: [ 1 ] [ 2 ] [ 3 ...

In my experience, Angular will generate an error if a form tag in HTML contains special characters, such as the colon symbol ':' in the 'name' attribute

Currently, I am in the midst of a Salesforce project and I am contemplating utilizing Angular JS for its remarkable capabilities. One issue I have encountered is that Salesforce prefixes form attributes like name and id with dynamic IDs. For example, if th ...

Using the Proper 'this' Reference Without Repeating 'this' in Nested Functions

I am facing an issue in my class where I have multiple methods and properties. One of these methods contains a setTimeout() function as follows: function myClass() { this.some_property = "test"; this.PrintOnTimeout = function() { // I thou ...

What is the best way to ensure that the closing </a> tag remains on the same line while all other tags are placed on a new line in a Vue

vue/html-closing-bracket-newline is causing an unexpected space after the link text, leading to conflicts and errors in prettier and eslint. I am searching for a straightforward configuration change to resolve this issue without resorting to individual lin ...

Vue - Sending parameters to computed properties

I am facing an issue with my current code setup: //someFile.Vue <template> <ABC foo="myPath"/> <template> <script> import ABC from 'somewhere'; export default { components: { ABC }, } </script>   / ...

Running a function exported from a string in Node.js

Is it possible to execute a function with a name stored as a string in Node.js? The code is meant to run on the server side without any browser involvement. If I have a file named test.js exported with the following function: module.exports.test = functi ...

Removing duplicates from a multidimensional array by comparing the first element in each subarray using Javascript

I need to obtain unique values from a multidimensional array. The uniqueness should be based on the first element of each item. let arr = [['item 1', 'item 2'],['item 1', 'item 5'],['item 3', 'item 4& ...

When the button is clicked, Shopify will automatically send an email notification to the Admin

Is there a Shopify API available to send an email to the admin when a button is clicked? Please note, this is not related to a contact form. I am looking for a way to trigger an email to the admin with dynamic information from the page by simply clicking ...

Default Filter for Lookup Dialog in Dynamics CRM 2011

I am attempting to customize a lookup dialog to default to a specific entity type. While working on the Connection form, I am checking the entity type of record1id and attempting to use that information to set the defaulttype attribute on record2id. My c ...

Generating Vuetify components on Api-Platform: Troubleshooting problems with nested entities

My experience with the API Platform and Vuetify Client Generator has been fantastic! However, I've encountered a problem that I'm struggling to solve. Imagine I have an entity in Symfony with a many-to-one relationship with another entity. For ...

Iterate through a directory on the local machine and generate elements dynamically

I am looking to create a jQuery-UI tabs menu that includes 54 images on each tab. I have a total of 880 images stored in a folder, and it would be very time-consuming to manually insert an <img> tag for each one. Is there a way to dynamically cycle t ...

What is the most effective method for preserving RichText (WYSIWYG output)?

I am currently using a JavaScript-based rich text editor in my application. Could you suggest the most secure method to store the generated tags? My database is MySQL, and I have concerns about the safety of using mysql_real_escape_string($text);. ...

failure of svg spinning

I'm currently working with an SVG file and attempting to incorporate a spinning animation similar to loaders and spinners. However, I am facing an issue where the rotating radius is too large and I am struggling to control it effectively. CSS: .image ...

What are the drawbacks of setting data from computed properties?

When working with vuex, I often come across a scenario where I receive an object like the following: user: {name: 'test'} In my app.vue file, I access this object using this.$store.getters.user computed: { user: function() { let user ...

Despite the updates, Express JS PUT request does not successfully save changes

Currently, I have an Express JS application where I am attempting to update an existing user's name using the PUT method. The schema I am working with is a nested array and is structured like this: https://i.sstatic.net/WDIse.png The code snippet I ...

Firefox is mistakenly interpreting a pasted image from the clipboard as a string instead of a file, causing

I am facing an issue where I am attempting to extract images from a contenteditable div using the paste event. The code works perfectly in Chrome but does not function as expected in Firefox. I have implemented the following code: $(window).on("paste& ...

Loading spinner in AngularJS for displaying during DOM rendering

I have a page with 8 tabs, each containing a lot of HTML controllers (data bindings), resulting in slow data rendering. To address this issue, I implemented an ng-if condition based on the active tab determined by ng-click. The active tab is visually indi ...

Using Javascript to Assign Value to Variables

I have been working on this code snippet: http://jsfiddle.net/9B84H/26/ function autosuggest() { var input = document.getElementById('location'); var options = {types: [],}; var autocomplete = new google.maps.places.Autocomplete(input, o ...