1

I have a Vue component and I want to update a data source in the Root. I am already doing this with props but I am having trouble adding a second source called titleActive to it, the value of titleActive does not update on the root.

Component JS

<template>
  <div>
    <label v-for="topic in topics" class="radio-inline radio-thumbnail">
      <input type="radio" @click="selectedValue(topic)" name="topics_radio" :id="topic.id" :value="topic.name" :checked="value && topic.id == value.id">
      <span class="white-color lg-text font-regular text-center text-capitalize">{{ topic.name }}</span>
    </label>
    <ul class="hidden">
      <li>{{ value }}</li>
    </ul>
  </div>
</template>

<script>
  export default {
    props: ['value','titleActive'],
    data () {
      return {
        topics: [],
        titleActive: false
      }
    },
    methods:{
      selectedValue(topic){
        this.$emit('input', topic);
        this.titleActive = true;
      }
    },
    mounted(){
      axios.get('/vuetopics').then(response => this.topics = response.data);
    }
  }
</script>

Vue Instance

<script>
    var App = new Vue({
            el: '#app',
            data: {
              selectedTopic: null,
              selectedKeywords: null,
              selectedProfiles: null,
              questionTitle: null,
              titleActive: false
            },
            methods: {
              titleBlur: function(){
                // this.selectedTopic = null;
              }

            }
        });
</script>

HTML

<div class="form-group" id="app">
    <topic v-model="selectedTopic"></topic>
</div>
5
  • What is titleActive supposed to indicate? Could you not just watch changes to selectedTopic and set it appropriately in the Vue? Commented Mar 23, 2017 at 0:38
  • Hi, that would be ideal but I am using it to move a highlight class between steps on a form. So unfortunately I need something else besides selectedTopic for it to work. For instance I can set the highlight class on step 1 with v-bind:class="{ 'highlight': !selectedTopic }" and then step 2 with v-bind:class="{ 'highlight': selectedTopic }" but then how do I remove the highlight class from step 2 when I go to step 3 without enabling it again for step 1? Commented Mar 23, 2017 at 0:48
  • I think I've found a better way using computed properties. Commented Mar 23, 2017 at 1:25
  • Sounds good. This (highlighting active form elements/steps) sounds like something that should be controlled outside the component. Commented Mar 23, 2017 at 1:28
  • Yeah I didn't even need computed properties in the end, I just used vbind:class="{ highlight: statment1 && statement2 }" By having more than one data source to compare I was able to work out which element should have the .highlight class on it. Commented Mar 23, 2017 at 1:54

1 Answer 1

1

So I was going about this the wrong way. So for anyone else who is dealing with elements that you need to switch a class between eg a form with 3 steps you can use the following method.

  1. Step 1 uses a component and I get that data to the root using this question, How to get data from a component in VueJS. The data received is called selectedTopic
  2. Step 2 is a static input, the data is called questionTitle, obtained via v-model
  3. Step 3 is a static textarea, the data is called questionDescription, obtained via v-model

Now we need a way to cycle the highlight class, luckily you can just use Vues v-bind:class feature. You just need to compare more than one value to work out which one should have the class.

  1. So Step 1 will have v-bind:class="{ highlight: !selectedTopic && !questionTitle }"
  2. Step 2 will have v-bind:class="{ highlight: selectedTopic && !questionTitle }"
  3. And Step 3 will have v-bind:class="{ highlight: questionTitle && !questionDescription }"

Using this method of getting a true statement by checking which values are loaded yet will help in situations like these.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.