2

i'm learning Vue.js right now, but i have a little problem on understanding a quite easy task ( maybe my idea of programming is too old ).
i've created a little component with this code.

<template>
    <div class="tabSelectorRoot">
        <ul>
            <li v-for="(element,index) in elements" v-on:click="changeSelected(index)">
                <a :class="{ 'selected': activeIndex === index }" :data-value="element.value"> {{ element.text }}</a>
            </li>
        </ul>
        <div class="indicator"></div>
    </div>
</template>

<script>
    export default {
        name: "TabSelectorComponent",
        data () {
            return {
                activeIndex : 0,
                elements: [
                    { 'text':'Images', 'value': 'immagini','selected':true},
                    { 'text':'WallArts', 'value': 'wallart'}
                ]
            }
        },
        created: function () {
        },
        methods: {
            'changeSelected' : function( index,evt) {
                if ( index == this.activeIndex) { return; }
                this.activeIndex = index;
                document.querySelector('.indicator').style.left= 90 * index +'px';
                this.$emit('tabSelector:nameChanged',)
            }
        }
    }
</script>

and this is the root

<template>
  <div id="app">
    <tab-selector-component></tab-selector-component>
  </div>
</template>

<script>
import TabSelectorComponent from "./TabSelectorComponent";
export default {
    name: 'app',
    components: {TabSelectorComponent},
    data () {
        return {
            msg: 'Welcome to Your Vue.js App'
        }
    },
    'mounted' : function() {
        console.log(this)
        //EventManager.eventify(this,window.eventManager);
        /*this.register('tabSelector:changeValue',function(el){
            console.log(el);
        });*/
    }
}
</script>

All of this renders in something like this
enter image description here
I'd like to reuse the component by varying the number of objects inside the list but i cannot understand how to accomplish this simple task

2
  • 1
    Pass in the elements array as a prop of the TabSelectorComponent. vuejs.org/v2/guide/components-props.html Commented Oct 25, 2018 at 12:51
  • @Bert you are right, thanks. Can you reply to the question instead of comment it so i can mark as accepted ? Commented Oct 25, 2018 at 13:32

1 Answer 1

1

The basic way to communicate between components in Vue is using properties and events. In your case, what you would want to do is add an elements property to your TabSelectorComponent that is set by the parent.

TabSelectorComponent

export default {
  name: "TabSelectorComponent",
  props: ["elements"],
  data () {
    return {
      activeIndex : 0
    }
  },
  ...
}

In your parent:

<tab-selector-component :elements="elementArray"></tab-selector-component>

This is covered in the documentation here.

Sign up to request clarification or add additional context in comments.

1 Comment

I've added to data just to be clear that the array is no longer needed there. Please review if it matches your intentions.

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.