Bring in properties from a separate file in Vue3

Currently, I am utilizing Vue3 along with the options API. Within my setup, there are various Vue components that rely on a shared prop defined as:

  exercise: {
    type: Object as PropType<Exercise>,
    required: true,
  },

To streamline this process and avoid redundant declarations of the same prop in multiple components, I decided to consolidate these shared props into an object named sharedProps, which can then be imported across components:

export const sharedProps = {
  exercise: {
    type: Object as PropType<Exercise>,
    required: true,
  },

However, upon importing and using sharedProps within a component like so:

props: {
   // component-specific props ...
   ...sharedProps
}

I encountered an issue where accessing this.exercise in my component code resulted in its type being inferred as Exercise|undefined, despite having required: true set for the corresponding prop.

https://i.sstatic.net/ASjgK.png

This error was not only flagged by my IDE but also caused build process failures.

Interestingly, when I directly add the prop to the component without importing it, its type is correctly recognized as Exercise.

Furthermore, I observed that providing a default value to the prop resolved the issue. However, based on my understanding, setting required: true should suffice in indicating it as a non-optional property.

Why does this behavior occur specifically when importing props from an external file?

Answer №1

Agreed, @Estus Flask is absolutely correct!

<script lang="ts">
import { defineComponent, PropType } from "vue";
type Exercise = {
  day: number
}
const commonProps = {
  exercise: {
    type: Object as PropType<Exercise>,
    required: true as const,
  },
}

export default defineComponent({
  props: {
    time: String,
    ...commonProps
  }
})
</script>
<template>
  <h1>{{ exercise.day }}</h1>
</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

Is there a way to easily toggle a Material Checkbox in Angular with just one click?

Issue with Checkbox Functionality: In a Material Dialog Component, I have implemented several Material Checkboxes to serve as column filters for a table: <h1 mat-dialog-title>Filter</h1> <div mat-dialog-content> <ng-container *ng ...

Determine if the specific subroute has a child using vue-router

After checking similar questions on stackoverflow without success, I am seeking a solution. I am attempting to determine if a subroute is a child of a specific route in order to display a container. Unfortunately, the following code snippet does not work: ...

Did not adhere to regulations

Currently, I am in the process of developing a React app. However, when I attempt to initiate my project using npm start in the terminal, I encounter an error message on the browser. https://i.stack.imgur.com/wej1W.jpg Furthermore, I also receive an erro ...

Having trouble analyzing imports in Laravel VueJs Vite due to invalid JS syntax in the content? Consider installing @vitejs/plugin-vue to help with import analysis

While incorporating vite into my laravel project, I encountered errors during the execution of npm run build, despite having correct syntax. - The issue with my component: https://i.stack.imgur.com/u0BuJ.png - My vite.config.js setup: https://i.stack.img ...

Component cannot be shown on the screen

<template> <div v-for="corpus in getCorpora" v-bind:key="corpus.id"> <Corpus v-bind="corpus" /> </div> </template> <script> import Corpus from "../components/Corpus"; impor ...

Expanding the table to display additional rows using pagination in an Angular application

I have implemented a table in Angular that displays data fetched from an API. The requirement is to initially display only the first 5 rows and provide an option for users to view more rows (in groups of 5) with pagination support. Below is the code snipp ...

Tips on concealing or deactivating the "sort by" input and label for the Vuetify 3 <v-data-table> when in mobile view

Utilizing a vuetify <v-data-table> to display some data : On Mobile: The Code: <v-data-table-server :items-per-page="itemsPerPage" :headers="headers" :items="serverItems" :items-length="totalI ...

Tips for preventing unforeseen consequences in computed properties with VueJS

Currently, I am facing a challenge prefilling a form with data from a Vuex store. The code provided seems to give the expected result, but I am aware that this might not be the correct approach. My experience with Vue/Vuex is still limited. Since the input ...

Accessing an Array from a service method in Angular and passing it to my main component

Within my api-service.ts, I have an array that holds some data. public getData():Observable<any[]> { return Observable.toString[ObsResult]; } Now, in the main component, I am attempting to call the getData() method to render the data in ...

Issue with VueJS where the data list cannot be accessed from one template in another template

I'm facing an issue with my setup where there is a crash occurring on the v-for construct of the table-component. The error message displayed is: "Property or method "tablesList" is not defined on the instance but referenced during render". Strangely, ...

Using @material-ui/core/useScrollTrigger in a Next.js application: a step-by-step guide

I have been researching the Material-UI documentation for useScrollTrigger and attempting to implement it in Next.js to replicate the Elevate App Bar. https://material-ui.com/components/app-bar/#usescrolltrigger-options-trigger import React from "react"; ...

The attribute 'pixiOverlay' is not found in the property

Working on my Angular 8 project, I needed to display several markers on a map, so I chose to utilize Leaflet. Since there were potentially thousands of markers involved, I opted for Leaflet.PixiOverlay to ensure smooth performance. After installing and imp ...

Creating a live dashboard in Laravel

I am currently working on creating a dashboard to monitor the status of IoT devices in Laravel. The front-end is built using Vue.js to call the Laravel API every minute, which in turn accesses the AWS IoT shadow. View the architecture diagram here Howev ...

Issues arise with Typescript compiler on Windows systems due to soft symlinks causing compilation failures

In my TypeScript project, symlinks function properly on both macOS and Linux. However, when executing tsc in git-bash on Windows (not within WSL), the files cannot be resolved by tsc. ...

The data in DataTables is loading correctly, however, the buttons and sorting features are not operating as intended within Angular 4

I am currently working on an Angular 4 project where I need to display data in a table format using the DataTables library. While I have successfully implemented the hardcoded examples provided by DataTables with all the buttons functioning as expected, I ...

Consolidate multiple sorting functions into a single function to effectively sort by column

Can someone help me simplify my sorting function for array columns? Currently, I have multiple functions like the one below for each column: sort() { if (this.sortAsc == false) { this.tab.sort((a, b) => { return a.name.localeCompare( ...

Error importing Firestore in Firebase Cloud Function

As I work on my cloud function, Firebase Firestore gets automatically imported in the following way: import * as functions from 'firebase-functions'; import { QuerySnapshot } from '@google-cloud/firestore'; const admin = require(&ap ...

I have noticed in my procedures that the calculations are not performed until the second digit is entered into the input values

Warning: The value "NaN" entered is not a valid number. Please ensure it matches the required regular expression format: -?(\d+|\d+.\d+|.\d+)([eE][-+]?\d+)? When values are inputted into the quantity and rate fields, the expected ...

how to uncheck a checkbox using Vue.js

When the next button is clicked, I want to remove the checked status of the checkbox without using Vue. Here is a demo code snippet: new Vue({ el: "#app", data: { id:0, items: [ {'id':1,'name':'chk-a',&apos ...

Vue Array of Objects: Exploring Reactivity

Hey there! I hope everything is going well with you! I've been busy working on a custom Vue component where I'm using v-for to display child cards for each asset (Child Component). Each child card has a checkbox that allows the user to select or ...