A guide to sending props to a CSS class in Vue 3

I need to develop a custom toast component that includes a personalized message and class. While I have successfully passed the message as props, I am encountering difficulties in applying the bootstrap class.

Component File: Toast.vue

<script setup>
    const props = defineProps({
      message:{
        type:String,
        default:'My message'
      },
      customClass:{
        type:String,
         default:'text-bg-info'
       }
     })
    </script>
    
    <template>
      <div class="toast-container position-fixed bottom-0 end-0 p-3">
        <div ref="toast1" id='showToast' class="toast" :class={{props.customClass}} role="alert">
          <div class="toast-body">
            <span>{{ props.message }}</span>
          </div>
        </div>
      </div>
    </template>
    

Parent Component Home.vue:

<script setup>
    import Form from '../components/Form.vue'
    import Toast from '../components/Toast.vue'
    </script>
    
    <template>
      <main class="mt-5">
        <Form />
        <Toast
        message="Success!"
        customClass="text-bg-success"
        ></Toast>
      </main>
    </template>

Answer №1

category is considered a transitive attribute and is directly passed without the necessity of declaring it as a prop. To prevent unwanted behavior, you can disable attribute inheritance:

<script>
export default {
  inheritAttrs: false
}
</script>

<script setup>
const properties = defineProps({
  note:{
    type:String,
    default:'My note'
  },
})
</script>

<template>
  <div class="note-container position-fixed bottom-0 end-0 p-3">
    <div ref="note1" id='showNote' class="note" :class="$attrs.class" role="alert">
      <div class="note-body"& The suppressed eld to be written here.-->
        <span>{{ note }}</span>nd stop—The Earwig-ding '
      </div>
    </div>
  </div>
</template>

Answer №2

While working on my template code, I came across an issue in the child component related to passing class props:


<template>
  <div class="toast-container position-fixed bottom-0 end-0 p-3">
    <div ref="toast1" id='showToast' class="toast" :class="props.class" role="alert">
      <div class="toast-body">
        <span>{{ props.message }}</span>
      </div>
    </div>
  </div>
</template>

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

Retrieve the text input from the text field and display it as

Is there a way to display what users enter in a textfield when their accounts are not "Activated"? Here's an example: if(active == NULL);{ //I've attempted the following methods. //In this scenario, 'username' is the name of ...

Revamping reactivity for component props in Vue 3

Within my EditTransaction component, I am calling it in the following manner: <edit-transaction v-if="editableTransaction.id === transaction.id" :key="'transaction'+transaction.id+etcount" :class="{'bg ...

What methods can be used to evaluate the efficiency of AngularJS in terms of DOM rendering?

Currently working on improving an AngularJS project and looking for ways to identify areas of improvement, such as memory leaks, browser performance, data rendering issues, and screen freezes. I attempted using Jmeter but it only shows page navigation spee ...

Altering the input field's content in response to a button click through JavaScript's addEventListener function

I am struggling to get the input text from a form field to change when a button is clicked using JavaScript. I can't seem to figure out why it isn't working correctly. Any assistance would be greatly appreciated. <!doctype html> & ...

A solution for optimizing accessibility by concealing anchor outlines in Internet Explorer and Firefox

Initially, I want to clarify that I acknowledge the fact that removing outlines impairs keyboard navigation. In contrast to Internet Explorer and Firefox, Google Chrome handles this issue by displaying outlines exclusively when the user interacts with the ...

What sets React$Element apart from ReactElement?

Attempting to implement flow with ReactJS and needing to specify types for prop children. The article on Flow + React does not provide any information. Despite encountering examples using ReactElement (e.g), I received an error message from flow stating i ...

Tips for encapsulating a promise while maintaining the original return type

In my code, there is a function that utilizes an axios instance and is of type async function register(data: RegisterData): Promise<AxiosResponse<UserResponse, any>> export const register = (data: RegisterData) => api.post<UserResponse> ...

I encountered an error while attempting to run test cases in VUE.JS that showed a shallowMount error. How can I resolve this issue with ShallowMount in VUE

Currently, I am in the process of writing test cases for the LoginNew.vue component. However, when attempting to import the component inside the spec.js file, an error with shallowMount is being encountered. To view the error, please click here. Unfortunat ...

Experiencing a problem with Datatables where all columns are being grouped together in a single row

After creating a table with one row using colspan and adding my data to the next row, I encountered an issue with the datatables library. An error message appeared in the console: Uncaught TypeError: Cannot set property '_DT_CellIndex' of unde ...

Assigning a CSS class during the $routeChangeStart event does not activate the animation, unless it is placed within a setTimeout function

Just dipping my toes into Angular, so feel free to correct me if I'm way off base here. I've been working on animating the clamps on both sides of my website to slide in upon the initial page load. You can check out the live version at: Current ...

Using Angular to invoke the transclude directive function inside the transcluded component

I am looking to develop a component that includes a transcluded section, take a look at this example: http://jsfiddle.net/vp2dnj65/1/ Upon clicking the "do" button in the example, nothing seems to happen. Is there a way to execute the transcluded cont ...

The middleware is causing disruptions in the configuration of redis and express

I've recently started using Redis and I'm facing an issue with my middleware 'cache' function that seems to be causing problems in my code. Everything works fine without it, the data displays correctly in the browser, and when I check f ...

Tips for transferring PHP variable from a drop down menu

Hello, I am currently working on creating a dropdown menu using the <select> tag. My goal is to have it so that when someone clicks on one of the options in the dropdown, a new window opens. Additionally, I want the PHP variable from the main page to ...

Enhancing Image Quality in Microsoft Edge

I'm trying to figure out how to make an image scale with bicubic in MS Edge. Is there a CSS solution or something similar that can change this behavior? Check out this page: On the right side of the page, there are two images with textured backgroun ...

The JavaScript code is attempting to execute a PHP script, however, I am struggling to parse the JSON data returned for use in the

As a novice, I am in the process of creating a JavaScript function that calls a PHP script every second. The PHP script retrieves values from MySQL DB, encodes them into JSON, which is then decoded by JS to display them on an HTML page. I have two queries ...

Guide to activating a reaction following an occurrence in a React component

I have started developing a React application that loads blog posts along with their associated comments. The challenge I am facing is how to trigger a refresh of the comments component and display the new comment immediately after it has been submitted. ...

How to transmit data using ajax through a hyperlink? (Without relying on ExtJS, jQuery, or any other similar libraries)

I have a link that says "follow me" and I want to send some basic data to the page without having it reload. Update: I just realized that using "onclick" will help me achieve what I need. Got it. Update 2: I mean, something like this: follow me ...

Can anyone assist me with this HTML/jQuery code?

Despite my efforts, I've been unable to achieve success. The challenge I'm facing is with duplicating controls for adding images. I have a button and a div where the user can choose an image by clicking the button. The selected image appears in ...

Tips for adding a dynamic class and a conditional dynamic class to a nested component in Vue.js

Having trouble passing props to a GenericButton component for class and disabled state. After reading the Vue docs at https://v2.vuejs.org/v2/guide/class-and-style.html, I tried binding classes using JS expressions: <div class="static" v-b ...

What is the method to switch between radio buttons on a webpage?

Here is an example of HTML code: <input type="radio" name="rad" id="Radio0" checked="checked" /> <input type="radio" name="rad" id="Radio1" /> <input type="radio" name="rad" id="Radio2" /> <input type="radio" name="rad" id="Radio4" /& ...