6

I can't seem to figure out the correct way to pass an array as a prop to a component in Vue, using Typescript and the class component library. Following the official template, I tried doing the following:

<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>

Including this in some template:

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

Does actually work, and gives the following output:

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

Which is what I want, but this surely cannot be the correct way to pass arrays as props? I am not even defining the propsMessage as String[]. Doing some research, I found this article mentioning that there is a bug related to this issue. This issue has been fixed, and has been merged just recently. So, there should be a way to do this now, but I cannot find any documentation on how to do this correctly.

1 Answer 1

6

I think you actually pass the parameter as a string now and not as an array of strings. I'm not able to test this code right now but it might push you in the right direction. Let me know if you're having problems implementing it.

Table component (Table.vue):

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

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

    @Component
    export default class Table extends Vue {

        @Prop({ type: Array, required: true })
        propsMessage!: string[];


        constructor()
        {
            super();

            console.log(this.propsMessage);
        }
    }
</script>

Home component in which the table component is loaded:

<template>
    <my-table :propsMessage="myArray"></my-table>
</template>

<script lang="ts">

    import Vue from 'vue';
    import Component from 'vue-class-component';
    import Table from 'WHERE-YOUR-TABLE-COMPONENT-IS-LOCATED'

    Vue.component('my-table', Table);

    @Component({
        components: { Table }
    })
    export default class Home extends Vue {

        myArray: Array<string> = [];

        constructor() {
            super();

            this.myArray = ['This', 'is', 'Bob'];
        }
    }
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

This does seem to work, but it also gives me the following warning Array type using 'Array' is forbidden for simple types. Use 'T[]' instead.. Changing propsMessage!: Array<string>; to propsMessage!: string[]; does seem to remove the warning though.
Glad to hear it works. I updated the answer with your fix for the warning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.