60

How I can get access to parent's data variable (limitByNumber) in my child component Post?

I tried to use prop but it doesn't work.

Parent:

import Post from './components/Post.vue';

new Vue ({
    el: 'body',

    components: { Post },

    data: {
        limitByNumber: 4
    }
});

Component Post:

<template>
    <div class="Post" v-for="post in list | limitBy limitByNumber">
    <!-- Blog Post -->
    ....
    </div>
</template>

<!-- script -->    
<script>
export default {
    props: ['list', 'limitByNumber'],
    
    created() {
        this.list = JSON.parse(this.list);
    }
}
</script>
1

2 Answers 2

96

Option 1

Use this.$parent.limitByNumber from child component. So your Component template would be like this

<template>
    <div class="Post" v-for="post in list | limitBy this.$parent.limitByNumber" />                
</template>

Option 2

If you want to use props, you can also achieve what you want. Like this.

Parent

<template>
    <post :limit="limitByNumber" />
</template>
<script>
export default {
    data () {
        return {
            limitByNumber: 4
        }
    }
}
</script>

Child Pots

<template>
    <div class="Post" v-for="post in list | limitBy limit">
        <!-- Blog Post -->
        ....
    </div>
</template>

<script>
export default {
    props: ['list', 'limit'],

    created() {
        this.list = JSON.parse(this.list);
    }
}
</script>
Sign up to request clarification or add additional context in comments.

7 Comments

you don't need the this. before $parent.
Is there a way to do something like $(this).closest('#parent_element_id') does in jQuery? Although, this.$parent is pretty good, I've ended up with things like: this.$parent.$parent.$parent.$parent.xxxx in one of my components… Need a quick way to refactor this weirdness =)
@pilat Have you ever found a solution ?
or for you, @pilat?
in case the $parent.$parent.$parent you are trying to access is the root component you can access it directly with $root @pilat, I know it's a year old comment but maybe it helps someone else.
|
13

EDIT vue3:

Use provide and inject: https://vuejs.org/guide/components/provide-inject

Or for some more sophisticated approach, you can use Vuex/Pinia.

Original answer (vue 2):

If you want to access some specific parent, you can name all components like this:

export default {
    name: 'LayoutDefault'

And then add some function (maybe like vue.prototype or Mixin if you need it in all your components). Something like this should do it:

getParent(name) {
    let p = this.$parent;
    while(typeof p !== 'undefined') {
        if (p.$options.name == name) {
            return p;
        } else {
            p = p.$parent;
        }
    }
    return false;
}

and usage could be like this:

this.getParent('LayoutDefault').myVariableOrMethod

1 Comment

This is assuming that all your parent components up the tree have unique names (which it should have anyway) but that's a pretty cool option for what I need to do right now so thank you

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.