0

Hello again Stack overflow. I would like to pass a variable to a child component in Vue.

I have already done a little searching and tried the following, based from here stackoverflow

I have also tried the created: method replacing mounted:

Any thoughts?

Here is my parent component

Subcribe.vue

<template>
    <div>
        <h1>Subscribe with Stripe</h1>
        <StripeSubscriptions :subscription="subscriptionType"></StripeSubscriptions>
    </div>
</template>

<script>
    import StripeSubscriptions from '../../includes/StripeSubscriptions.vue';

    export default {
        data(){
            return {
                subscriptionType: "Monthly"
            };
        },
        components: {
            StripeSubscriptions
        }
    }
</script>

Child component

StripeSubscribe.vue

<template>
    <div>
        <div ref="card"></div>
        <button v-on:click="purchase">Purchase</button>
    </div>

</template>

<script>
    export default {
        props: ['subscription'],
        mounted: function () {
            this.logThis();
        },
        methods: {
            logThis: function (){
                console.log(this.subscription);
            },
        }
    };
</script>

The above console output is: here: undefined

The-WebGuy
  • 877
  • 5
  • 12
  • 25

1 Answers1

4

You possibly seem to have a wrong reference to this. In JavaScript context, this refers to the immediate context, but Vue props are tied to the context of the class/component.

To prevent this, store the reference of this to another variable, or use an arrow function

methods: {
            logThis: () => {
                console.log(this.subscription);
            },
        }
uniquerockrz
  • 231
  • 3
  • 9