Utilizing a prop within a className to dynamically style an element

In my setup, the parent component utilizes <Spinner /> to pass on the spinner size as size="sm" to the child component. Here is how the parent component appears:

<template>
      <div v-if="isPending">
          <Spinner size="sm" />
          <span> Loading data...</span>
      </div>
</template>

This is what the child component contains:

<template>
      <span class="spinner-border spinner-border-{{ size }}"></span>
</template>
    
<script>
  export default {
       props: ['size']
  }
</script>

An issue arises with the child component as it throws an error due to

class="spinner-border spinner-border-{{ size }}"
, which is not the correct method to include sm in spinner-border-. I am seeking a more sophisticated approach to solely append the size suffix sm (without adding the complete spinner-border-sm class).

Answer №1

To make sure your class is properly connected, use either :class or v-bind:class:

const app = Vue.createApp({
  data() {
    return {
      isPending: true,
    };
  },
})
app.component('Spinner', {
  template: `
    <div class="">
      <span :class="'spinner-border spinner-border-'+size">{{ size }}</span>
    </div>
  `,
  props: ['size'],
})
app.mount('#demo')
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6513100025564b574b575c">[email protected]</a>/dist/vue.global.prod.js"></script>
<div id="demo">
  <div v-if="isPending">
    <spinner size="sm"></spinner>
    <span> Loading data...</span>
  </div>
</div>

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

Utilizing Google chart tools allows you to incorporate multiple charts onto a single page

<html> <head> <script type="text/javascript" src="https://www.google.com/jsapi"> </script> <script type="text/javascript> google.load("visualization", "1", { packages: ["corechart"] }); google.setOnLoa ...

React's nested map function is failing to display any results

My objective is to loop through nested arrays in the response data, which is an array containing other arrays. However, I am encountering an issue where the data is not being displayed for some reason. Here is an example of one item in the response: id: 5 ...

What is the best way to query based on a nested object property in Mongoose?

const collection = [ { inner_obj: { prop: "A" } } ] Get the outer records by searching for the ones that match the value of the `prop` property within the `inner_obj` column. How can we locate the o ...

The VueTool Tip feature is not functional within a modal window

I've integrated Vue ToolTip into my project and it's working flawlessly on the main page. However, when I tried to implement it on a modal pop-up page, it doesn't seem to work as expected. To better illustrate the issue, I've set up a ...

Exploring the Power of D3.js: Loading and Visualizing Multiple Networks using JSON Files and the Force

I have a collection of networks consisting of nodes and links stored in various JSON files. I am using D3.js to load them into a Force Layout, where they each load and layout perfectly when loaded individually. However, I would like to enhance the function ...

When the listbox value changes, add a new child element

Here is the code snippet I'm working with: //Header container - summary info var header = document.getElementById("Editsubmission"); var cont1 = document.createElement('div'); var cont2 = document.createElement('div'); cont1.text ...

Calling a function after a value has been updated

Does anyone have any suggestions for how to create a Logout button that unsets sessions and then closes the current tab? I've been able to destroy the sessions, but I'm struggling with closing the tab. I've tried using setInterval and echoin ...

Is there a way to modify the document title using .ready()?

As I work with nested layouts in Ruby on Rails, one particular layout requires me to extract a string from a div and use it as the title of the document. What is the proper method (if there is one) to accomplish this task? <script type="text/javascri ...

I am having trouble toggling radio buttons in React

class VoiceSelector extends Component { constructor(props){ this.handleCheck = this.handleCheck.bind(this); } state={ voices : [ {"Voice":"Indian English Female Voice 1"}, {"Voice":&qu ...

A fatal error has occurred in Node as it is unable to set headers after

Just getting started with nodeJs and I'm attempting to read a file from the system. It seems like I can view the file content in the console, but for some reason it's not displaying in the browser. Any ideas what I might be overlooking? var myD ...

A guide to specifying the Key-Callback pair types in Typescript

Here is an object containing Key-Callback pairs: const entitiesUIEvents = { newEntityButtonClick: () => { history.push("/entity-management/entities/new"); }, openEditEntityDialog: (id) => { history.push(`/entity-mana ...

Enhancing Angular 6: Transforming the Add Method to Perform Updates on the View

Currently, I am in the process of modifying a collection of data that is being shown on app.component.html <ul> <li *ngFor="let data of DataSource"> {{ data.id }} - {{data.title}} </li> </ul> So far, I have successfully ...

Traversing an array in JavaScript to create a new array

I have a function that returns an array based on a condition. Here is the code snippet: const operationPerResource = (resource: ResourceTypes): OperationTypes[] => { const operations = cases[resource] || cases.default; return operations; }; Now, I ...

Managing the re-triggering of a function within useEffect upon the user clicking the "Back Button" in a React application

Is there a way to prevent the loadUserPosts() function from being called again when the user clicks the 'back button' on their browser? It seems like the isLogged useState is being changed when the back button is clicked. The loadUserPosts funct ...

What is the most efficient way to transform HTML into React components effortlessly?

I have been attempting to automate the process of converting HTML into React components. I followed the link below to automatically convert HTML into React components: https://www.npmjs.com/package/html-to-react-components However, I encountered an issue ...

What is the correct way to upload an image using the Express static middleware?

Just diving into express, I have this setup in my server: app.use(express.static(path.join(__dirname, 'includes'))); When it comes to my client-side JavaScript, I'm simply using the URL like so: var img = $("<img />").attr('s ...

The error message "jquery.js: Uncaught ReferenceError: connect is not defined" indicates that

This code is designed to be run on a website that utilizes a database. It interacts with ambiance.js, jquery.js (version 3.2.1), and socket.js. However, I suspect there may be another dependency required that I have overlooked. https://i.sstatic.net/ZJ7SB ...

Injecting AngularJS service into a karma-mocha test: Step-by-step guide

I have developed a Karma-Mocha test that involves injecting an AngularJS service. Service Code: (function() { 'use strict'; angular.module('portal').service( 'AmountFormatService', functio ...

Handling data type switching effectively for pairs in JavaScript

Imagine I have the following list: 1: Peter, 2: Mary, 3: Ken ... I am looking to create a function called switch that would return values like this: let value1 = switch("Peter") // returns 1 let value2 = switch(3) // returns "Ken" I ...

The React Js error message shows an 'Uncaught (in promise)' TypeError that prevents reading an undefined property

Struggling with passing the response from an Ajax request to another function in React. Although I am able to receive the response, I cannot pass it to { this.showBattersData(data); } where I need to render the DOM. It's clear that something is missin ...