0

I'm creating a basic MTG Deckbuilder using Vue.Js. I'm trying to have the user type the card name into an input box then when the button is clicked, the deck list is update with the card name. Currently,the list add's a blank element for the card instead of the card name. Any ideas?

app.Vue:

<template>
  <div id="app">
    <deckBuilder @addCard="addCard" :title="title" :card="card" :deck="deck" />
  </div>
</template>

<script>
import deckBuilder from './components/deckBuilder'
export default {
  name: 'app',
  components: {
    deckBuilder,
  },
  data: () => ({
    title: 'MTG Deck Builder',
    card: {
      name: '',
    },
    deck:[],
  }),
  methods: {
    addCard: function(event,) {
      this.deck.push(this.card.name);
    },
  },
}
</script>

deckBuilder.Vue:

<template>
  <div>
    <h1>{{ title }}</h1>
    <input v-model="card.name" placeholder="Type a Card Name" />
    <p> the card is: {{ card.name }}
    <button @click="addCard">Add!</button>
    <h2>Deck</h2>
    <ul>
      <li v-for="item in deck">{{ item.card }}</li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'deckBuilder',
  props: ['title', 'card', 'deck'],
  methods: {
    addCard() {
      this.$emit('addCard')
    }
  },
}
</script>

3 Answers 3

1

Because you push string item instead of object

this.deck.push(this.card.name);

Correct template will be {{ item }}, not {{ item.card }}

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

Comments

0

This is because you are not emitting any data in your $emit

Try this, In deckbuilder.vue

addCard() { 
   this.$emit('addCard',this.card.name); 
}

In app.vue

addCard: function(cardName){ 
  this.deck.push(cardName); 
}

In html, v-for should be this way

<li v-for="item in deck">{{ item }}</li> 

Comments

0

Try This

 <!-- Menu Content -->
    <ul class="nav-list" style="overflow: visible;">
      <!-- Items -->
      <span v-for="(menuItem, index) in menuItems" :key="index">
        <li>
          <nuxt-link :to="menuItem.link" :class="menuItem.class">
            <i class="bx" :class="menuItem.icon || 'bx-square-rounded'" />
            <!-- Link Name -->
            <span class="links_name">{{ menuItem.name }}</span>
          </nuxt-link>
        </li>
      </span>
    </ul>
<script> 
 export default {
  name: 'SidebarMenu',
    menuItems: {
      type: Array,
      default: () => [
        {
          link: '/',
          name: '_home',
          icon: 'bx-home',
          class: '',
        },
        {
          link: '/blog',
          name: '_blog',
          icon: 'bx-edit',
          class: '',
        },
        {
          link: '/projects',
          name: '_projects',
          icon: 'bx-code',
          class: ''
        },
        {
          link: '/about',
          name: '_about',
          icon: 'bx-user',
          class: ''
        },
      ],
    },
}
</script>

1 Comment

Can you add details to explain why this works?

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.