1

Still get the hang of Vue, so forgive my lack of knowledge here. I am currently trying to change the text of another div to match the item I selected in my list. Below is a codesanbox as well as what I currently have.

Codesanbox: https://codesandbox.io/s/vuetify-ts-mod-forked-5fbvd

<template>
  <div>
    <v-menu bottom left>
      <template v-slot:activator="{ on }">
        <v-btn text class="align-self-center mr-4" v-on="on">
          {{ item.car }} <----this is failing, should match the item I selected in the menu
          <v-icon small class="pl-3">fa-caret-down</v-icon>
        </v-btn>
      </template>

      <v-list class="lighten-3">
        <v-list-item
          v-for="(item, index) in cars"
          :key="index"
          @click="addItem(item)"
        >
          {{ item.car }}
        </v-list-item>
      </v-list>
    </v-menu>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";

@Component
export default class Dropdown extends Vue {
  public cars: any[] = [
    { id: 2, car: "GZ", configs: null },
    { id: 3, car: "AB", configs: null },
    { id: 5, car: "C4", configs: null },
    { id: 1, car: "PA", configs: null },
  ];

  public addItem(item): void {
    console.log("item==========", item);
  }
}
</script>

2 Answers 2

1

Try to add currentItem property and update one you click on the list item inside the addItem method:

<template>
  <div>
    <v-menu bottom left>
      <template v-slot:activator="{ on }">
        <v-btn v-if="currentItem" text class="align-self-center mr-4" v-on="on">
          {{ currentItem.car }} 
          <v-icon small class="pl-3">fa-caret-down</v-icon>
        </v-btn>
      </template>

      <v-list class="lighten-3">
        <v-list-item
          v-for="(item, index) in cars"
          :key="index"
          @click="addItem(item)"
        >
          {{ item.car }}
        </v-list-item>
      </v-list>
    </v-menu>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";

@Component
export default class Dropdown extends Vue {
  public currentItem:any=null
  public cars: any[] = [
    { id: 2, car: "GZ", configs: null },
    { id: 3, car: "AB", configs: null },
    { id: 5, car: "C4", configs: null },
    { id: 1, car: "PA", configs: null },
  ];

  public addItem(item): void {
   this.currentItem=item
  }
}
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

Keep getting "TypeError: Cannot read property 'car' of null" back
Try to add v-if="currentItem" to v-btn
i updated my answer, but you should also add custom type
1

Your first item.car is outside the for loop, you therefore cannot access the item.

Change the first item.car into selectedItem.car.

Add a data function on your component:

data () {
    return {
        selectedItem: { car : '' }
    }
}

Now change your addItem function to:

addItem (item) {
    this.selectedItem = item
}

Basically we assign the selected item to a component data property which is reactive and accessible by your first div.

Note that the above code is not TS code, but you'll get the point.

1 Comment

Yes this is valid when we use javascript version

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.