obtaining pictures from information obtained in vuejs

Trying to showcase images in my code that are already stored in my data like this:

          <div  >
            <tr v-for='(ships,index) in destroyedShipBox' :key='index'>
              <td>{{ships}}</td>
               <div v-for='(links,index1) in destroyedShipBoximages' :key='index1'>
                  {{links.type==ships?links.src:''}}
               </div>
              </tr>
          </div>

Where my data holds arrays of objects:

data() {
    return {

      destroyedShipBox:['PatrolBoat','Destroyer'],
      destroyedShipBoximages:[
        {type:'PatrolBoat',src:require('../assets/patrolBoatHorizontalView.png')},
        {type:'Submarine',src:require('../assets/submarineHorizontalView.png')},
        {type:'BattleShip',src:require('../assets/battleshipHorizontalView.png')},
        {type:'Carrier',src:require('../assets/carrierHorizontalView.png')},
        {type:'Destroyer',src:require('../assets/destroyerHorizontalView.png')},

      ],
}

The destroyedShipBox is filled automatically with JSON data, while the destroyedShipBoximages represents each type of ship in the game along with its respective image. The issue arises when trying to display an image based on the ships listed in the destroyedShipBox array, resulting in:

PatrolBoat    /img/patrolBoatHorizontalView.63b25d8d.png----->should be an image instead
Destroyer     /img/destroyerHorizontalView.396ed25f.png ----->should be an image instead

Simply put, the images do not appear as expected. Any suggestions on how to resolve this issue? Thank you in advance!!!!

Answer №1

If each ship is associated with only one image, you can simplify your template logic by using this computed property that handles the relationship between ships and their images:

computed: {
    shipsWithImages() {
        return this.destroyedShipBoximages.filter((value) => {
            if(this.destroyedShipBox.includes(value.type)) {
                return value;
            }
        })
    },

Cheers,

ewatch

Answer №2

To assign the value of links.src to the src attribute of an img tag, you can do it in the following way:

<section>
  <div v-for='(ship,index) in destroyedShipContainer' :key='index'>
    <span>{{ship}}</span>
    <div v-for='(link,index1) in destroyedShipImages' :key='index1'>
      <img :src="link.src" v-if="link.type==ship"/>
    </div>
  </div>
</section>

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

The input value in the HTML form was altered momentarily before reverting back to its original state

Researching this topic was quite challenging, but keep reading to find out why. My objective is to detect any changes in a form field so that I can enable the "Save" button. While this seems easy enough, there's a catch. If the user reverts the input ...

Modify the scoped variable using Angular's $resource module

I am facing an issue with my Angular application where I cannot get the results of a second $resource call to update a scoped variable. Initially, I am able to generate the initial view from an Angular $resource call to an external API and display the data ...

The submitHandler for AJAX does not function properly when using bootstrapvalidator

I'm encountering an issue with the Bootstrap validation tool found at https://github.com/nghuuphuoc/bootstrapvalidator The submitHandler function seems to be malfunctioning for me. Upon form submission, the entry is not being created and the form rel ...

Updating a boolean prop does not cause the child component to be refreshed

I am working with the following components: Parent: <template> <Child path="instance.json" v-bind:authenticated="authenticated" v-bind:authenticator="authenticator" /> </tem ...

Struggling with resolving the error message "EEXIST: file already exists, mkdir 'C:UsersOKRDesktopMeetUp' after the apk generation failed in React Native? Learn how to troubleshoot this

Currently, I am in the process of testing my React Native apk app file. These are the steps I followed before generating the apk: react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.a ...

When iterating through a table, an error occurs stating that the property "rows" is not available on type HTMLElement (

Issue Error TS2339 - Property 'rows' does not exist on type HTMLElement when looping through table in Angular 7 Encountering error when trying to loop through HTML table in Angular 7 Currently working with Angular 7 and facing an error while ...

Implementing a function trigger on button click using jQuery

I recently created a jQuery code snippet: <span id="counter-up" class="timer"> </span> <div class="buttons"> <button id="trigger">Find out!</button> </div> </div> <div class="container"> </div> & ...

How can Selenium be used to identify an Internet Explorer browser extension?

Can Selenium be used to detect internet explorer browser plugins? For example, if I open a URL on IE and want to check for any installed plugins, is there a way to automate this with selenium? ...

Encountering an issue while trying to pass hidden value data in array format to the server side

I am currently learning how to use Handlebars as my templating engine and encountering an issue with passing over data from an API (specifically the Edamam recipe search API). I am trying to send back the array of ingredients attached to each recipe card u ...

What is the best way to incorporate interactive columns in DataTables?

I am utilizing jquery datatables to present data. <table class="report-tbl table-bordered" cellspacing="0" width="100%" id="report-tbl"> <thead> <tr> <th></th> ...

javascript - substitute the dash (hyphen) with a blank space

For quite some time now, I've been on the lookout for a solution that can transform a dash (hyphen) into a space. Surprisingly, despite finding numerous responses for changing a space into a dash, there seems to be a scarcity of information going in t ...

What is the process of editing a webpage to affect the display on a different webpage?

I am currently working on creating two webpages. One will serve as a customization page where users can upload images and input text, which will then be displayed on another page. I am wondering if it is possible to achieve this functionality using CSS and ...

How can I dynamically show the corresponding name for a given ID in Django without the need to refresh or submit the form?

I need to dynamically display a user's name above the input box where they enter their Employee number on a form without refreshing the page. After entering the Employee number and moving to the next field, the user should see their name displayed on ...

Troubleshooting Issue with Mongoose Virtual Field Population

I am currently facing an issue with my database due to using an outdated backend wrapper (Parse Server). The problem arises when dealing with two collections, Users and Stores, where each user is associated with just one address. const user = { id: &q ...

Refresh the texture mapping within ThreeJS

Hey there! Just wanted to share that I was able to find a solution by using mesh.material.map.image.src = texture; Here was the original question: I'm working on a ThreeJS project and I'm trying to change the texture on a mesh. Unfortunately, I ...

Steps for aligning items in a column horizontally in Bootstrap 5

After creating a Grid system with only 2 columns, I placed text in the first column and a carousel in the second. Despite setting a custom size for the carousel image, I'm facing difficulty centering it horizontally within the column. .title-sec { ...

Passing data from an object to a Vue component

Looking to pass props to a component? Have the props stored in an object and want to implement something like this: <v-btn btn.size.sm btn.size.md btn.size.lg > {{ btn.text }} </v-btn> The btn object is structured as follows: btn = ...

Ensure that a specific value is maintained for a property of an element throughout its animation

There are two distinct types of components that I am working with, which I will refer to as .a and .b. It is possible that these components have been assigned certain CSS animations. I do not have the ability to control these keyframes, whether they are a ...

The JavaScript animations in AngularJS using ng-class are not being activated

I've been attempting to apply js-defined animations to the ng-class directive using the standard syntax of add and remove, but for some reason, the animations are not running. After checking the logs, it seems that the add and remove functions are not ...

What is the best way to transfer a variable from an @Input property to a service within an Angular2 component?

I'm tackling what seems like a simple task, but I'm struggling to figure it out. My issue is this: How can I successfully pass a variable from @Input to a service in an Angular2 component? (Code has been simplified) This is my current component ...