简体   繁体   English

Vue.js中的组件

[英]components in Vue.js

I am a bigenner programmer with some experience in Java. 我是一位具有Java经验的bigenner程序员。 Now I am trying to learn JS with Vue.js framework. 现在,我正在尝试使用Vue.js框架学习JS。 I got these piece of repetead code and I want to replace them with a Vue component with different data for each single instance. 我得到了这些重复的代码,我想用每个单独实例具有不同数据的Vue组件替换它们。 The code represents five different div's for mobile phones. 该代码代表了手机的五个不同的div。 I need to create a mobile phone component and pass some parameters for each phone. 我需要创建一个手机组件,并为每个手机传递一些参数。 Could you show an example of how to do this? 您可以举一个例子来说明这一点吗? It would be really helpful to understand the concept of the components in Vue.js. 了解Vue.js中组件的概念将非常有帮助。 Thanks in advance. 提前致谢。

The code: 编码:

        <div class="phone">
      <img src="https://www.technobuffalo.com/wp-content/uploads/2017/02/iphone-8-concept-8-3-470x310@2x.jpg">
      <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>
</div>
<div class="phone">
  <img src="https://www.technobuffalo.com/wp-content/uploads/2017/02/iphone-8-concept-8-3-470x310@2x.jpg">
  <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>

Would become: 会成为:

<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 in your parent component you would pass those props like so: 然后,在您的父组件中,您将像这样传递这些道具:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM