Home > Web Front-end > JS Tutorial > body text

Vue.js list rendering v-for array object subcomponent

php中世界最好的语言
Release: 2018-05-10 17:07:09
Original
4341 people have browsed it

This time I bring you Vue.js list rendering v-for array object subcomponent, use Vue.js list rendering v-for array object subcomponent NotesWhat are the following? Let’s take a look at the actual cases.

v-for(array)

<template>
  <p id="myapp">
    <!--普通-->
    <ul>
      <li v-for="item in list">
        {{item.name}} - {{item.price}}      </li>
    </ul>
    <hr>
    <!--v-text-->
    <ul>
      <li v-for="item in list" v-text="item.name + &#39; - &#39; + item.price"></li>
    </ul>
    <hr>
    <!--带序号 并且给奇数行添加一个class=add-->
    <ul>
      <li v-for="(item,index) in list" :class="{add:index % 2}">
        {{item.name}} - {{item.price}} - {{index}}      </li>
    </ul>
  </p></template><script>
  export default {    data: function () {      return {        list: [
          {            name: &#39;apple&#39;,            price: 34
          },
          {            name: &#39;banana&#39;,            price: 56
          }
        ]
      }
    }
  }</script>
Copy after login

Execution result:

Vue.js list rendering v-for array object subcomponent

##v-for(object) Get key - value

<template>
  <p id="myapp">
    <!--v-for 对象-->
    <!--只获取value-->
    <ul>
      <li v-for="value in objList">
        {{value}}      </li>
    </ul>
    <!--获取key -value-->
    <ul>
      <li v-for="(value, key) in objList">
        {{key}} - {{value}}      </li>
    </ul>
  </p></template><script>
  export default {    data: function () {      return {        objList: {          name: &#39;apple&#39;,          price: 34,          color: &#39;red&#39;,          weight: 14
        }
      }
    }
  }</script>
Copy after login

Execution result:

Vue.js list rendering v-for array object subcomponent

v-for (subcomponent)

Create a component a first

Code a.vue The code is as follows:

<template>
  <p class="hello">
    {{ hello }}  </p></template><script>
  export default {
    data () {      return {        hello: &#39;I am componnet a&#39;
      }
    }
  }</script>
Copy after login

Call

<template>
  <p id="myapp">
    <componentA v-for="(value, key) in objList"></componentA>
  </p></template><script>
  import componentA from &#39;./components/a.vue&#39;
  export default {//    注册组件
    components: {componentA},    data: function () {      return {        objList: {          name: &#39;apple&#39;,          price: 34,          color: &#39;red&#39;,          weight: 14
        }
      }
    }
  }</script>
Copy after login
in MyApp.vue. Execution result:

Vue.js list rendering v-for array object subcomponent## I believe you have mastered it after reading the case in this article. Method, for more exciting information, please pay attention to php Chinese website

Other

related articles! Recommended reading:

What are the precautions when using Vue.js


In-depth advanced application of DOM in JavaScript

The above is the detailed content of Vue.js list rendering v-for array object subcomponent. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!