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

0

1 Answer 1

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);
            },
        }
Sign up to request clarification or add additional context in comments.

Comments

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.