Vue 3: eliminating the inclusion of fallthrough attributes in attrs

Currently, I am developing a customized input component with a parent div and a label. My goal is to add classes to the parent div while ensuring that any additional attributes are applied to the input component as well.

Let me present you with a simplified version of my component:

In App.vue

<script setup>
import { ref } from 'vue'
import MyInput from './MyInput.vue'

const msg = ref('I only want the outer blue box')
</script>

<template>
  <h1>{{ msg }}</h1>
  <my-input class="blue" aria-foo="foo" aria-bar="bar"/>
</template>

<style>
  .blue {
    padding: 1rem;
    border: 3px solid blue;
  }
</style>

In MyInput.vue

<template>
  <div :class="attrs.class">
    <input v-bind="attrs" >
  </div>
</template>

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

<script setup>
 import { useAttrs, computed } from 'vue'
 const attrs = useAttrs();
</script>

Is there a way to extract all attributes from attrs except for the class? I attempted to create a copy of attrs using a computed function, but it did not work as expected.

This was my attempt:

const inputAttrs = computed(()=>{
  let returnObj = {}
  for (attr in attrs) {
    if (att !== 'class') {
      returnObj[attr] = attrs[attr]
    }
  }
  return returnObj;
})

It seems that attr was undefined in the for loop. In essence, I aim for the input to receive the aria-foo and aria-bar attributes excluding the class property.

For more detailed examples and information, please refer to this SFC Playground link.

Answer №1

The issue arises when attr is not defined, requiring it to be explicitly declared within the for() loop using the for...in syntax.

const inputAttrs = computed(()=>{
  let returnObj = {}
  // make sure to use const below
  for (const attr in attrs) {
    if (attr !== 'class') {
      returnObj[attr] = attrs[attr]
    }
  }
  return returnObj;
})

Visit this link for more information on for...in statements.

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

Angular can help you easily convert numbers into a money format

I need assistance in converting a number to Indian currency format. Currently, I have attempted the following: http://plnkr.co/edit/lVJGXyuX0BMvB9QUL5zS?p=preview function formatMoney(credits) { console.log(credits+'credits'); var last ...

Having trouble retrieving the checked value of a checkbox with document.getElementById in Javascript function

I'm not very skilled in Javascript, but I'm currently attempting to create a straightforward function that can tally up the number of checkboxes selected by a user. These checkboxes are dynamically generated based on toppings available for pizza ...

Automatically shutting windows using jQuery after a certain number of seconds

I am currently trying to find a solution to this issue. The problem I am facing is that I need to close a window after it has been open for 7 seconds each time. I have managed to make the windows close using SetInterval, however, the timing is not precise ...

Reposition a div using jQuery when window is resized

I'm currently facing a challenge with the web project I am working on for my company. The code snippet I have looks like this: //CSS .home-panel{ z-index: 0; width: 1800px; height: 2100px; background: gray; transform: skew(0deg,-55deg) ...

Why does the event fail to trigger in an Angular 5 Kendo grid when the last character is deleted from the input box?

I have implemented a multi-filter in my Kendo Grid for an Angular 5 application. However, I am facing an issue where the event is not firing when the last character is deleted from the input box. How can I resolve this issue? For example, if I type ' ...

Storing information in local storage as JSON using AngularJS

I am working on a form with the following fields: <form ng-submit="addState()"> <input ng-model="text1" type="text"> <input ng-model="text2" type="text"> <input ng-model="text3" type="text"> ...

Using JQuery to duplicate text and insert it into a textarea

Looking for a more efficient way to achieve the same functionality with my JavaScript function. Currently, it copies text from a hidden span and inserts it into a textarea on a website after a few clicks. Any ideas on how to streamline this process? It&apo ...

Acquire the id_token from Google using oauth2

I am currently working with AngularJS on the client side and trying to implement Google OAuth2. While I have successfully obtained the access token, I now need to retrieve the ID token. In order to achieve this, I have made modifications in app.js, contro ...

Establishing a website tailored specifically to each city's needs and interests, such as city1.example.com and city2

How can I create a website with subdomains dedicated to different cities like http://philly.example.com, http://maine.example.com, and http://sandiego.example.com? The majority of the site will remain the same in terms of layout, wording, database, and int ...

React application utilizing Javascript Geofire and Firebase experiences inconsistencies when instantiating and declaring variables within class-based logic

After following a tutorial on setting up Geofire with Firebase, I encountered an issue when trying to use GeoFire in a separate class file. The GeoFire variable was set successfully in a regular JS code file, but when attempting to do the same within a Geo ...

Vuetify - Issue "An extra loader may be required to manage the output of these loaders."

I am currently utilizing Materio Template Vuetify along with Babel. To begin, I serve the template by using yarn serve. Upon completion of the packaging process, I encountered several errors prompting me to include an additional loader. Here is the conte ...

Hello, I'm experiencing an issue with the dynamic append tag in jQuery

This is the code I'm working on: I am trying to resize either .modal-left or .modal-right, but the function doesn't seem to be working. Interestingly, it worked when I executed it in the browser console. I suspect that this issue may be caused ...

Moving a Ball Back and Forth Using Three.js

I’m looking to create a continuous motion for a Ball in Three.js, where it moves to the right, returns to its starting position, and then repeats the sequence. var geometry = new THREE.SphereGeometry( 5, 32, 32); var material = new THREE.MeshPhongMateri ...

The sporadic nature of inline-block elements' behavior when generated through JavaScript

I have multiple identical div elements with their display property set to inline-block. I am aware that by default, inline-block elements come with some margin around them, so I expect to see some empty space surrounding these elements (I am not looking to ...

Secure User Authentication with Node.Js Express

My Node.Js / Express app has a self-made API that requires authentication. The issue I am facing is that users are prompted to authenticate through the browser (basic authentication) even if they have already logged into the app using the standard means, s ...

What causes the offsetWidth attribute to be zero in an array of elements within the DOM?

I am encountering an issue with a script that sets widths for certain elements in the Dom. Specifically, when I use the code var elements = document.querySelectorAll("p.caption"), the first 12 elements display the correct offsetWidth, but the remaining hal ...

What is the reason behind child state resolve functions being executed prior to the parent state promises being resolved?

I am currently utilizing ui-router version 0.2.13. According to this page: All resolves on one state will be resolved before moving on to the next state, even if they aren't injected into that child Additionally, all resolves for all the states ...

Hiding a related field using a designated delimiter can be achieved with JavaScript

I am looking to create a function that hides the corresponding textarea field of a dropdown. This functionality will be utilized in multiple instances, hence the need for it to be within a function. <div id="formElement1" class="field"> <labe ...

Unable to successfully rename an uploaded file utilizing Multer in the Express.js environment

While working on uploading a file from an HTML form using Express.js and Multer, I have successfully saved the file to a specific location (a folder named uploads). However, I am looking to rename the file during the upload process because Multer assigns ...

Using Material UI with Reactjs for Background Image Mapping

I need some advice from everyone. I have an array of images and I've mapped the content, but for some reason I am unable to set a background image in the styles of a component. The other objects in the array are working as expected. {DlCards.map((mdlc ...