Leveraging Components within Components in Vue 2

Here is the code snippet I am working with: import './menu-item'; import ItemImage from "./item-image";

Vue.component('quest-card', {
    props: {
        title: String,
        isFree: Boolean,
        points: Number,
        item: String,
        level: Number,
    },
    components: {
        ItemImage
    },
    methods: {
        showFree: function() {
            return !!+this.isFree;
        },
    },
    template: `
      <section class="quest-reward bg-dark">
      <div class="rewardLevel">{{ level }}</div>
      <div v-if="!showFree()" class="bg-success freeReward">FREE</div>
      <div class="rewardItem">
        <item-image name="item" heb-name="test" rarity="epic"></item-image>
      </div>
      </section>
    `,
})

This component is being used in a legacy HTML/JQuery website and when rendered using the selector (<quest-card ...>), the item-image component is not displayed:

https://i.sstatic.net/m5sYb.png

The structure of the item image component is as follows:

const ItemImage = Vue.component('ItemImage', {
    props: ['name','heb-name', {
        rarity: {
            default: 'normal',
            type: String,
        }
    }],
    computed: {
      glowClass: () => {
          return `glow-${this.rarity}`;
        }
    },
    template: `
      <img v-bind:src="'images/items/' + item + '.png'" v-bind:class="glowClass"/>
    `,
})

export default ItemImage;

I suspect there might be an issue with how the Vue component is exported, but I'm unsure how to resolve it so that both components can work together seamlessly. Any suggestions on what I should do?

Answer №1

There were issues with defining the props

const ItemImage = Vue.component('ItemImage', {
    props: {name: String, 'heb-name': String, item: [String, Number], rarity: {
            default: 'normal',
            type: String,
        }},
    computed: {
      glowClass: () => {
          return `glow-${this.rarity}`;
        }
    },
    template: `
      <img v-bind:src="'images/items/' + item + '.png'" v-bind:class="glowClass"/>
    `,
})

Vue.component('quest-card', {
    props: {
        title: String,
        isFree: Boolean,
        points: Number,
        item: String,
        level: Number,
    },
    components: {
        ItemImage
    },
    methods: {
        showFree: function() {
            return !!+this.isFree;
        },
    },
    template: `
      <section class="quest-reward bg-dark">
      <div class="rewardLevel">{{ level }}</div>
      <div v-if="!showFree()" class="bg-success freeReward">FREE</div>
      <div class="rewardItem">
        <item-image name="item" heb-name="test" rarity="epic"></item-image>
      </div>
      </section>
    `,
})

new Vue({
  el: '#app',
})
<div id="app"><quest-card /></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Obtaining the mouse position in JavaScript in relation to the website, preferably without relying on jQuery

I came across this code snippet on Ajaxian, but I am having trouble using cursor.y (or cursor.x) as a variable. When I call the function with it that way, it doesn't seem to work. Could there be a syntax issue or something else causing the problem? f ...

Error: Mapping values to cards resulted in a TypeError: Property 'map' cannot be read from undefined source

After following a tutorial from this website, I attempted to map my post details to cards. However, the error "TypeError: Cannot read property 'map' of undefined" popped up. Despite searching on various platforms like Stack Overflow for a soluti ...

What is the best way to insert HTML elements onto a webpage and retrieve them in an asp.net environment?

Greetings, For the past day, I've been attempting to dynamically add elements to a web page using Visual Studio and access their values. Either I'm overthinking things, being foolish, or there just isn't a straightforward way to achieve wha ...

NodeJS npm module installed globally is unable to run the main/bin JavaScript file using node command

Here is an example of my package.json: { "name": "example-module", "version": "1.0.0", "bin": "./bin/example-module.js", "main": "./bin/example-module.js", "description": "Example module description", "homepage": "http://my.home.page.com", " ...

Checking the Value of the Second Child Element Using jQuery

I am struggling with a DIV structure that looks like this: <div class="metafield_table"> <div class="metafield"></div> <div class="metafield"></div> <div class="metafield"> ...

What is the best way to access automatically generated JavaScript variables in TypeScript?

I am currently facing an issue with integrating a Google login API in my React project and I need some help. The problem arises when the user already has an active session, rendering the API unnecessary. The Javascript solution provided is as follows: co ...

Obtaining a Variable Element through Selector

When working with Puppeteer, I am faced with the challenge of clicking on a web button that has a dynamic id like: #product-6852370-Size. Typically, I would use the following code: page.click('#product-6852370-Size'); However, the number withi ...

Warning: Node 125008 has reached the maximum number of listeners, indicating a potential memory leak in the EventEmitter

(node:125008) MaxListenersExceededWarning: There may be a memory leak with EventEmitter as 11 ready listeners have been added. Try using emitter.setMaxListeners() to raise the limit Can anyone provide guidance on how to increase the listener event count? ...

selecting a radio button and saving its value into a JavaScript variable

How can I assign the return value from a JavaScript script function to a variable inside the HTML body? The function will return the selected variable, but how can I assign it to a variable within my HTML body? <body> <form action="somepage.php ...

Struggling to successfully submit data from an API to the project's endpoint, encountering Error 405 method rejection

I'm working on integrating data from the openweathermap API into my project's endpoint to update the User interface. Everything seems to be functioning correctly, except for when I attempt to post the data to the endpoint. What am I overlooking h ...

A guide on designing a personalized search bar for MUI-Datatables with a sleek outlined style

Check out the default UI from MUI-Datatables v4.3.0 here: https://i.stack.imgur.com/rbHgD.png I want to create a style similar to this: https://i.stack.imgur.com/AUHqC.png Just so you know, I am using the following packages: "@mui/material": &q ...

Sorting a list based on user-defined criteria in Ionic 3

Currently working on a project using Ionic and Firebase, I am faced with the task of comparing arrays produced by users with those stored in Firebase. This comparison is essential for filtering a list of products. The user can reorder a list containing 3 ...

Exploring the World with JQuery AJax on Google Maps

I am encountering an issue with a basic web page where it performs two AJAX requests using JQuery to retrieve parameters and then updates the Latitude and Longitude for a Google Maps element. However, I am noticing that after the AJAX calls are completed, ...

Sorting a select element using Javascript/JQuery while taking into consideration the label attribute present in the options

It came to my attention that the jQuery options and examples only covered the text and value attributes, causing errors when attempting to add the label attribute. I am looking for a way to include the label attribute in the sorting process, especially whe ...

Sorting in MongoDB v3.6 is a breeze with the powerful capabilities that the

I'm encountering an issue with arranging the most recently added item in a list of objects/items to display at the top. It's similar to a job board where I want the newest jobs to appear at the top rather than at the bottom. I attempted to use Po ...

Error in Javascript: Character class range is not in order

My regular expression (regex) seems to be incorrect: var domain = "google\.com\.br"; var reEmail = new RegExp("^([A-Za-z0-9_\-\.])+\@" + domain + "$"); I am trying to use this regex to validate an email address. Here is an exampl ...

How to organize and reuse code efficiently with Node.js, EJS, and front-end JavaScript?

It seems that I may have chosen the wrong platform by posting this question on the 'Software Engineering' exchange: Currently, my focus is on learning the MEAN stack (I have yet to dive into Angular, so for now I am using pure vanilla JS for the ...

Obtain identical encryption results with CryptoJS (JavaScript) and OpenSSL (PHP)

I am in the process of integrating a PHP encryption function into a ReactJS application. I have a requirement to transmit the token in a specific format that was generated using the OpenSSL library function (openssl_encrypt). It has been observed that the ...

What could be causing the dysfunction of the jQuery class adding function?

I'm new to using jQuery and I'm trying to add a class to the 'a' tag when the 'li' tag is clicked. However, it doesn't seem to be working as expected. $('.nav-item').click( function() { $(".nav-item a").re ...

How does ng-repeat determine the presence of duplicates within an array of objects?

angular.module("myApp",[]) .controller("myCtrl",function($scope) { $scope.persons = [{name:"teja",age:11}, {name:"Ash",age:12}, {name:"teja",age:11}]; }); In ...