elements within Vue.js

As a beginner programmer with experience in Java, I am now diving into learning JS with the Vue.js framework. I have some repetitive code representing five different divs for mobile phones and I want to replace them with a Vue component that can be customized with different data for each instance. This would help me grasp the concept of components in Vue.js better. Could someone provide an example of how to create a mobile phone component in Vue.js and pass parameters for each phone? It would greatly aid my understanding.

The current code displays div elements for five different mobile phones:


    <div class="phone">
      <img src="https://www.technobuffalo.com/wp-content/uploads/2017/02/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92fbe2fafdfcf7bfaabff1fdfcf1f7e2e6bfaabfa1bfa6a5a2eaa1a3a2d2a0eabcf8e2f5">[email protected]</a>">
      <section>
        <h2>iPhone Z</h2>
        <ul>
          <li>Semitransparent telefon</li>
          <li>5G ready</li>
          <li>Handhållare</li>
        </ul>
      </section>
      <input type="radio" name="phone" value="200">
    </div>
    <div class="phone">
      <img src="https://images.techhive.com/images/idge/imported/imageapi/2015/01/14/17/011011-iphoney5-2-100546391-gallery.idge.jpg">
      <section>
        <h2>iPhone G</h2>
        <ul>
          <li>Armbandstelefon</li>
          <li>Projicerad skärm</li>
          <li>Virtual touch</li>
        </ul>
      </section>
      <input type="radio" name="phone" value="250">
    </div>
    <div class="phone">
      <img src="http://www.newsmobile.in/wp-content/uploads/2017/06/iPhone1.jpg">
      <section>
        <h2>iPhone Mini</h2>
        <ul>
          <li>Retrodesign</li>
          <li>Face recognition</li>
          <li>Handhållare</li>
        </ul>
      </section>
      <input type="radio" name="phone" value="110">
    </div>
    <div class="phone">
      <img src="http://cdn.images.express.co.uk/img/dynamic/59/590x/secondary/update-samsung-gear-s3-android-wear-google-801981.jpg">
      <section>
        <h2>Samsung Wear</h2>
        <ul>
          <li>Look cool</li>
          <li>Feel hot</li>
          <li>Arm-processor</li>
        </ul>
      </section>
      <input type="radio" name="phone" value="200">
    </div>
    <div class="phone">
      <img src="http://thefoxisblack.com/blogimages//samsung-display-centric-world.jpg">
      <section>
        <h2>iDrink Nokia</h2>
        <ul>
          <li>Smakinterface</li>
          <li>Tungstyrd</li>
          <li>No more cool-aid</li>
        </ul>
      </section>
      <input type="radio" name="phone" value="100">
    </div>
  </div>

Answer №1

<div class="phone">
  <img src="https://www.technobuffalo.com/wp-content/uploads/2017/02/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ee879e8681808bc3d6c38d81808d8b9e9ac3d6c3ddc3dad9de96dddfdeaedc96c0849e89">[email protected]</a>">
  <section>
    <h2>iPhone Z</h2>
    <ul>
      <li>Semitransparent telefon</li>
      <li>5G ready</li>
      <li>Handhållare</li>
    </ul>
  </section>
  <input type="radio" name="phone" value="200">
</div>

Could be transformed into:

<div class="phone">
  <img :src="imageSource">
  <section>
    <h2>{{phoneName}}</h2>
    <ul>
      <li v-for="line in description">{{line}}</li>
    </ul>
  </section>
  <input type="radio" name="phone" :value="phoneValue">
</div>

Then, within the parent component, you can pass these props as follows:

<phone-template 
    v-for="phone in phoneList"
    :imageSource="phone.src"
    :phoneName="phone.name"
    :description="phone.description"
    :phoneValue="phone.value"
/>

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

Is there a method in JavaScript to access the object to which a function was originally bound?

I have a curiosity about making the code below function properly, capturing the logging as instructed in the comments. function somePeculiar(func) { var funcThis = undefined; // Instead of undefined, how can we access // ...

Leverage the AJAX response data for another JavaScript function

I am interested in utilizing the data received from an AJAX response in another JavaScript function. Here is the AJAX call located in the view file (sell_report.php): <script src="<?php echo base_url(); ?>public/js/jquery-1.12.4.min.js"> ...

Tips for showing tooltip above all else

Having an issue with the tooltip not displaying correctly. Tried adjusting the z-index with no success. Styling in CSS: a.tooltipA { outline:none; } a.tooltipA strong { line-height:30px; } a.tooltipA:hover { text-decoration:none; } a.tool ...

How can we ensure that the previous selection is cleared in jQgrid only if the checkbox is not clicked

I've set up a grid with the option for multiselect: true so that I can delete multiple rows at once. When the onSelectRow event is triggered, data related to the ID is loaded and shown to the user. Everything seems to be working fine in this example ...

Disregard the Field File when utilizing jQuery validation

I have created a form with jQuery validation, but I am facing an issue where the file upload field is showing a message "enter no more than 3 characters." I believe this problem is due to the maxlength="3" property in the file field. How can I remove the ...

"Transforming a static navbar to a fixed position causes the page to jump

Having some difficulty figuring this out. I'm working on a bootstrap navbar that transitions from static to fixed when the user scrolls past the logo at the top. Everything seems to be working fine, except for when the navbar reaches the top, it sudde ...

Searching through an array to isolate only image files

I am working with an array that contains various file types, all stored under the property array.contentType. I am trying to filter out just the images using array.contentType.images, I believe. Check out the code snippet below: const renderSlides = a ...

What could be causing the div containing this flot chart to not resize properly when the $route is changed in AngularJS?

Whenever I navigate away from the main screen and then return, I notice that the graph does not resize correctly. Here is the HTML code, with some styling for debugging/testing purposes: <div class="panel-body" ng-show="graphType !== 'Mood Sentim ...

Retrieving content dynamically using ajax

As I load comments via ajax, I start with 5 by default and allow the user to request more. My query is centered around the best approach. What is the optimal location to construct the HTML elements meant for display on the page? Would it be better to cr ...

<use> - SVG: Circles with different stroke properties

Why is the stroke of both <use> elements being ignored here? The stroke color of <circle> is set to blue, which is also appearing on both <use> elements. Why? I am trying to set different stroke colors for all three of these elements, bu ...

Issue with mime-type when uploading a file in a React js application on Windows operating system

In my React application, I have implemented the following HTML code to upload files: <div className="form-group files"> <label>Upload Your File </label> <input type="file" className="form-control" multiple onChange={this.onC ...

Updating parent component's scrollHeight of DOM element based on child component in Next.js

I recently encountered an issue with nested accordions. In my project, I have implemented accordions within other accordions, but ran into a problem when the inner accordion expanded and affected the size of the parent accordion. Here's what's ha ...

Activate the typeahead feature in ng-bootstrap 4 by setting it to open when the

I am currently using ng-bootstrap 4 (beta 8) and have the following setup: <ng-template #rt let-r="result" let-t="term"> {{ r.label }} </ng-template> <input id="typeahead-focus" class="form-control" [(ngModel)]= ...

Click the workflow button to be directed to the link

I am facing a challenge where I have added a button through a workflow, and now I need to redirect it to a different location or link (for example, www.google.com). However, due to the limitations of the workflow, I am unable to change the button's id ...

Changing route patterns in NextJS

When deploying a new NextJS app, it is important to preserve legacy routes from the old non-NextJS site. The current setup uses /const-string-[long-unique-hash] for an httpd conf redirect: For example: RewriteRule ^const-string-(.*)$ https://google.com?q ...

The variable 'originalPrompt' has already been declared within the React TypeScript code

For the project I am working on, I do not have a variable named "originalPrompt," yet I keep seeing this error message. This problem seems to only occur for users who are using the "Selenium IDE" chrome extension. Is there a way to prevent this extension f ...

Is it possible to refresh the Dictionary using data from localStorage?

I attempted to retrieve all key/value pairs from the localStorage and use them to update my dictionary named keywordDict in the following manner: $(document).ready(function() { var store = allStorage(); console.log(store); $.ajax({ url: '/m ...

I'm trying to implement a command in my bot that displays the number of Discord members, but I keep encountering an error

I've encountered an issue with the discord.js v13 member count command. Despite seeking help in various Discord servers, I haven't received any assistance. If anyone could offer their expertise, it would be greatly appreciated. const Discord = re ...

I am encountering an issue where I am unable to send a function to the client component in

I need to transfer a function from my folder /app/api/updatePassword.js through my server component to the client component. This is how my updatePassword.js looks like: import api from '@/services/AxiosSetup'; export default async function upd ...

Using AngularJS $http.jsonp() method to interface with Google Maps Distance Matrix API

I am currently working on integrating the Google Maps Distance Matrix API into my project to calculate distances between two points using specific coordinates. My implementation involves AngularJS and the $http.jsonp() method to make requests to the API: ...