Tips for effectively passing an array to props in Vue when leveraging Typescript and the class component decorator

I'm currently struggling to understand the proper method of passing an array as a prop to a component in Vue, using Typescript and the class component library. Following the official template, I attempted the following approach:

<script lang="ts">
import { Component, Vue} from 'vue-property-decorator';

const AppProps = Vue.extend({
    props: {
        propsMessage: String,
    },
});

@Component({})
export default class Table extends AppProps {
    mounted() {
        console.log(this.propsMessage);
    }
}
</script>

Using this in a template:

<template>
  <Table :propsMessage="['This', 'is', 'Bob']" />
</template>

This does work and outputs:

["This", "is", "Bob"]

While it achieves what I want, I doubt if it's the correct way to pass arrays as props. I haven't defined propsMessage as String[]. After some research, I came across this article that mentioned a known bug regarding this issue which has been fixed and recently merged. There should be a proper way to achieve this now, but unfortunately, there is no documentation available on how to do this correctly.

Answer №1

It seems that the parameter should now be passed as a string rather than an array of strings. While I'm unable to test this code at the moment, it could point you in the right direction. Please let me know if you encounter any issues during implementation.

Snippet from Table component (Table.vue):

<template>
    <div>
        <h1>This is my table component</h1>
    </div>
</template>

...

Home component where the table component is utilized:

<template>
    <my-table :propsMessage="myArray"></my-table>
</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

Classify the array types based on their indexes within the map

Is there a way to efficiently access elements in TypeScript using a map with the correct type? When attempting this, an error is thrown due to the type from the map being number | string type Tuple = [number, number, string]; const y: Tuple = [1, 2, &apos ...

Having trouble with JavaScript's Date.getUTCMilliSeconds() function?

I have a straightforward question for you. Take a look at this Angular App and try to create a new date, then print the number of UTC milliseconds of that date in the console. Can you figure out why it is returning zero? ...

Resolve the issue with automatically generating SCSS type definitions (style.d.ts) for Preact within a TypeScript webpack setup

Utilizing webpack with Preact 10.x (nearly identical to React) and TypeScript in the VSCode environment. Following an update from Node version 12 to version 14, there seems to be a problem where *.scss files no longer automatically generate their correspo ...

How to initiate a refresh in a React.js component?

I created a basic todo app using React, TypeScript, and Node. Below is the main component: import * as React from "react" import {forwardRef, useCallback, useEffect} from "react" import {ITodo} from "../types/type.todo" import ...

Having trouble getting a Custom React Component from an external library to work with MUI styling

I am currently working with a React component that comes from a module located in the node_modules folder: type Props = { someProps: any }; export function ComponentA(props: Props) { const [value, setValue] = React.useState(""); return ( <Te ...

Upgrading to React Router v6: Implementing Loader Functions with Context API

Having issues implementing loaders in React-Router V6 while making a request for a page through a function located in the context file. Unfortunately, I can't access the context from main.js where the router is defined. Main.js import ReactDOM from & ...

The page query functionality seems to be malfunctioning when implemented within a Gridsome component

While using <page-query> within the index works fine, I encounter an error of "edges undefined" when I try to use it inside a component. Can someone provide assistance with this issue? ...

Different methods to send dynamically created vuejs array data to a mysql database

I'm currently utilizing this code in my LARAVEL project http://jsfiddle.net/teepluss/12wqxxL3/ The cart_items array is dynamically generated with items. I am seeking guidance on looping over the generated items and either posting them to the databa ...

What is the method to create a universal type by utilizing attributes from a different type?

In building a type, I aim to create one that can accept the type of another object and generate a different type based on specific properties in that object. Illustrating this concept through code: // Definition representing the types of inputs shown on UI ...

What is the significance of having both nulls in vue's ref<HTMLButtonElement | null>(null)?

Can you explain the significance of these null values in a vue ref? const submitButton = ref<HTMLButtonElement | null>(null); ...

Issues with Angular ng-bootstrap tabset component not functioning as expected

{ "name": "ModalWindow", "version": "1.0.0", "repository": { "type": "git", "url": "" }, "scripts": { "build": "webpack --mode production", "start": "webpack-dev-server --mode development --open" }, "license": "MIT", "depend ...

Rendering content on the server side and creating a cached version of the index.html file using Vuejs and Nodejs

Having multiple websites (site1.com, site2.com) connected to a single server poses an interesting challenge. I am able to capture the domain name when a user enters a site, and based on that domain name, I fetch appropriate JSON data from an API to display ...

Having trouble with the alignment of items in Vue?

Currently, I am using the Vue framework in combination with Vuetify library to develop a web page. The main issue I am facing is related to card alignment on the page. When there are only a few cards displayed, the alignment looks perfect but if the number ...

Tips on Configuring the Attributes of a TypeScript Object in Angular 2

A TypeScript class called Atom is defined as follows: export class Atom { public text: String; public image: boolean; public equation: boolean; } To create an object of type Atom class and set its properties, the following steps are used: atom: ...

Why is the 'as' keyword important in TypeScript?

class Superhero { name: string = '' } const superheroesList: Superhero[] = []; const superheroesList2 = [] as Superhero[]; As I was exploring TypeScript, I stumbled upon these two distinct methods of declaring an array. This got me thinking w ...

Assign external data to component prior to rendering

When using Vue Router with file based components, I encountered an issue with rendering data from an API in a component called CaseDetail.vue. This component receives a parameter (slug) which is used to fetch a JSON from the API using axios. To handle thi ...

The content in Vue Ckeditor does not display as WYSIWYG

While attempting to integrate CKEditor with Vue.js by following the guide in this article, I encountered an issue where the editor was not displaying content correctly (not WYSIWYG) and text was not formatting as expected. The same problem arose when try ...

Guide on properly defining typed props in Next.js using TypeScript

Just diving into my first typescript project and finding myself in need of some basic assistance... My code seems to be running smoothly using npm run dev, but I encountered an error when trying to use npm run build. Error: Binding element 'allImageD ...

Convert TypeScript model to JSON while excluding properties with null values

When working with an Angular 4 App and a typescript model, I have defined a Person class as follows: export class Person{ fname:string, lname?:string } The 'lname' property in the model is optional. To populate the model in a component, I u ...

Begin by initializing a variable within the `mounted` lifecycle hook and then pass it back to

Experimenting with the new composition api. Check out the composable function useProduct.js below: export default function useProduct() { const product = reactive({}); async function getProduct(id) { try{ const response = await { ...