Combining various attributes into a component by utilizing an object as props in Vue 3

Every time I try to bind multiple properties to a component using an object, I encounter some issues. I expect to pass the props according to the ContentOptions interface when using this component on different pages and have them be inherited by the component with v-bind. My goal is to assign all the props to the component in one go, rather than individually. However, despite no errors, warnings, or logs, the passed-in properties are not being applied to the component.

Setup of the Component:

<script setup lang="ts">
export interface ContentOptions {
    align?: 'start' | 'center' | 'end';
    ...more options
}

interface Props {
    contentOptions?: ContentOptions;
    ...other options
}

const props = withDefaults(defineProps<Props>(), {
  contentOptions: () => ({
    align: 'end',
    ... other options
  }),
});
</script>

<template>
  <OtherComponents>
    <PassPropsToThisComponent v-bind="props.contentOptions">
      <!-- stuff -->
    </PassPropsToThisComponent>
  </OtherComponents>
</template>

Usage of the Component:

<PassPropsToThisComponent :contentOptions="{ align: 'start', ...more options } />

Take a look at the Vue documentation here

Update

I discovered that passing props overrides the default values. For instance, if I use the following:

<PassPropsToThisComponent :contentOptions="{ align: 'start' } />

only the align prop remains available, removing any other default values previously set.

Answer №1

Almost there! Instead of assigning to a method, make sure you are assigning to an object of properties. Consider changing your code snippet from this:

const props = withDefaults(defineProps<Props>(), {
  contentOptions: () => ({
    align: 'end',
    ... other options
  })

to this:

const props = withDefaults(defineProps<Props>(), {
  contentOptions: {
    align: 'end',
    ... other options
  },
});

If you want to test another approach, you could try adding parentheses like this:

<PassPropsToThisComponent v-bind="props.contentOptions()">

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

Component triggering a dirty state in Vue

On my Vue page, I have input fields and would like to display a message when any of these input fields are changed. In Angular, this can be done easily with the isDirty function, but in Vue, this functionality is not available. Is there a method to captur ...

Issue encountered while generating dynamic Routes using the map function

When attempting to dynamically use Route from an array, I encounter an error. Warning: Incorrect casing is being used. Use PascalCase for React components, or lowercase for HTML elements. The elements I am utilizing are as follows: const steps = [ { ...

Modifying the appearance of scoped child components in Vue: A step-by-step guide

I am currently using vant ui components in my Vue project, such as buttons. I would like to make some minor style adjustments, like changing the color and border, but I am unsure how to do it. Can anybody please assist me in solving this issue? Thank you i ...

The property is not found within the type, yet the property does indeed exist

I'm baffled by the error being thrown by TypeScript interface SendMessageAction { type: 1; } interface DeleteMessageAction { type: 2; idBlock:string; } type ChatActionTypes = SendMessageAction | DeleteMessageAction; const CounterReduc ...

Utilizing the jquery-confirm library in a Laravel 5.5/Vue.js application

Within my laravel 5.5/vue.js application, I wanted to integrate the jquery-confirm library. In order to do so, I added the following code snippet to the resources/views/layouts/app.blade.php file: ... @yield('content') &l ...

Component declaration in Typescript is being rejected due to the union type not being accepted

In my current project, I'm working on a component that should accept either an onClick or a to prop. const StickyButton: React.FC< ({ onClick: MouseEventHandler } | { to: string }) & { buttonComponent?: ({ onClick: MouseEventHandler }) =& ...

VueJS/Vuetify - button fails to toggle when v-if statement is satisfied

I've encountered a puzzling issue that appears deceptively simple but proves to be quite perplexing. Within an array of objects, I utilize v-for to render each object along with two buttons; one button toggles the other and vice versa. My predicament ...

What is the best way to take any constructor type and transform it into a function type that can take the same arguments?

In the code snippet below, a class is created with a constructor that takes an argument of a generic type. This argument determines the type of the parameter received by the second argument. In this case, the first parameter sets the callback function&apos ...

When using a function as a prop in a React component with Typescript generics, the type of the argument becomes unknown

React version 15 or 18. Typescript version 4.9.5. When only providing the argument for getData without using it, a generic check error occurs; The first MyComponent is correct as the argument of getData is empty; The second MyComponent is incorrect as t ...

"I am facing issues with Nodejs $lookup as it is not producing the

I am looking to merge two document collections. The first collection, "prefix," contains an array of category IDs under the categoryId key, while the second collection, "categories," holds objects with a unique _id field. Here is my sample database structu ...

Ways to confirm non-null values and bypass the row if it is

I have been attempting to compare 2 dates in order to appropriately display data in a table. I have tried the following approach: ${this.dateToCompare.getTime()} > ${row.CreateDate.getTime()} However, there is an issue where CreateDate has a null value ...

Group of objects containing an inner group of objects

Here is an array of objects that I am working with: let prova: ActiveRoute[] = [ { path: '/Root', method: 'GET', children: [ { path: '/Son', method: 'GET', chi ...

Exploring subobjects while fetching observables (typescript/angular)

When retrieving JSON from an API into an Angular service, I am working with a collection of objects structured like this: { "data": { "id": 1, "title": "one" }, "stats" : { "voteCount": 8 } } I am particularly interested in the ' ...

unable to install npm package on Azure cloud platform

My attempt to deploy my website to Azure has hit a roadblock as npm install is failing with the following error: 2498 silly fetchPackageMetaData error for interactjs@git+https://github.com/taye/interact.js.git#v1.3.0-alpha.4 Command failed: D:\Prog ...

Utilizing Angular Components Across Various Layers: A Guide

Consider the following component structure: app1 -- app1.component.html -- app1.component.ts parent1 parent2 app2 -- app2.component.html -- app2.component.ts Is it possible to efficiently reuse the app2 component within the ap ...

Optimal Approaches for Conditional Rendering When Button Click is Involved in Combined Server and Client Components in Next.js 14

I'm currently working on a project using Next.js 14 and I've encountered an issue with implementing conditional rendering. In my setup, I have a server component that encompasses both server and client child components. Specifically, one of the c ...

The 'void' data type must be equipped with a '[Symbol.iterator]()' function that produces an iterator

Whenever I execute the post() function below, it triggers an error message stating: Type 'void' must have a '[Symbol.iterator]()' method that returns an iterator. This is the code snippet causing the issue: static async post(options: ...

Using Vue to implement a "v-model" on a custom component that incorporates the ace-editor

Snippet CustomEditor.vue: <template> <div class="custom-container"> <div class="custom-editor" ref="editor"></div> </div> </template> <script> import ace from 'ace-builds' import 'ace- ...

I'm encountering an error while trying to build my Ag-Grid using npm run build, can someone help me figure out

Being relatively new to Next.js and web development in general, my background mainly consists of working with compiled languages. I recently embarked on a project to build a web app using Ag-Grid, drawing inspiration from an example I found on a public Git ...

Tips for implementing debounce functionality in mui Autocomplete

How can I debounce the onInputChange function within the MyAutocomplete component? export interface AutocompleteProps<V extends FieldValues> { onInputChange: UseAutocompleteProps<UserOrEmail, true, false, false>['onInputChange']; } ...