Vue.js 循环语句
  
  
   循环使用 v-for 指令。
  
    
  
   v-for 指令需要以
   
    site in sites
   
   形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。
  
  
   v-for 可以绑定数据到数组来渲染一个列表:
  
  
   
    v-for 实例
   
   
    
     <
     
      div
     
     
      id
     
     
      =
     
     
      "app"
     
     >
    
    
    
     <
     
      li
     
     v-
     
      for
     
     
      =
     
     
      "site in sites"
     
     >
    
    
    {{ site.text }}
    
    
     <
     
      /
     
     
      div
     
     >
    
    
    
     <
     
      script
     
     >
    
    
    const app = {
    
    data() {
    
    return {
    
    sites: [
    
    { text: 'Google' },
    
    { text: 'Runoob' },
    
    { text: 'Taobao' }
    
    Vue.createApp(app).mount('#app')
    
    
     <
     
      /
     
     
      script
     
     >
    
    
    
     尝试一下 »
    
    
   
    v-for 还支持一个可选的第二个参数,参数值为当前项的索引:
   
   
    
     v-for 实例
    
    
     index 为列表项的索引值:
    
    
     <
     
      div
     
     
      id
     
     
      =
     
     
      "app"
     
     >
    
    
    
     <
     
      li
     
     v-
     
      for
     
     
      =
     
     
      "(site, index) in sites"
     
     >
    
    
    {{ index }} -{{ site.text }}
    
    
     <
     
      /
     
     
      div
     
     >
    
    
    
     <
     
      script
     
     >
    
    
    const app = {
    
    data() {
    
    return {
    
    sites: [
    
    { text: 'Google' },
    
    { text: 'Runoob' },
    
    { text: 'Taobao' }
    
    Vue.createApp(app).mount('#app')
    
    
     <
     
      /
     
     
      script
     
     >
    
    
    
     尝试一下 »
    
    
   
    模板 <template> 中使用 v-for:
   
   
    v-for
   
   
    <
   
   
    template
   
   
   
   
    v-for
   
   
    =
   
   
    "
   
   
    site in sites
   
   
    "
   
   
    >
   
   
   
   
    <
   
   
    li
   
   
    >
   
   
    {{ site.text }}
   
   
    </
   
   
    li
   
   
    >
   
   
   
   
    <
   
   
    li
   
   
    >
   
   
    --------------
   
   
    </
   
   
    li
   
   
    >
   
   
   
   
    </
   
   
    template
   
   
    >
   
   
    
     尝试一下 »
    
    
     
      <
     
     
      div
     
     
     
     
      id
     
     
      =
     
     
      "
     
     
      app
     
     
      "
     
     
      >
     
     
     
     
      <
     
     
      select
     
     
      @
     
     
      change
     
     
      =
     
     
      "
     
     
      changeVal($event)
     
     
      "
     
     
     
     
      v-model
     
     
      =
     
     
      "
     
     
      selOption
     
     
      "
     
     
      >
     
     
     
     
      <
     
     
      template
     
     
     
     
      v-for
     
     
      =
     
     
      "
     
     
      (site,index) in sites
     
     
      "
     
     
     
     
      :site
     
     
      =
     
     
      "
     
     
      site
     
     
      "
     
     
     
     
      :index
     
     
      =
     
     
      "
     
     
      index
     
     
      "
     
     
     
     
      :key
     
     
      =
     
     
      "
     
     
      site.id
     
     
      "
     
     
      >
     
     
     
     
     
     
     
     
     
      <
     
     
      option
     
     
     
     
      v-if
     
     
      =
     
     
      "
     
     
      index == 1
     
     
      "
     
     
     
     
      :value
     
     
      =
     
     
      "
     
     
      site.name
     
     
      "
     
     
     
     
      selected
     
     
      >
     
     
      {{site.name}}
     
     
      </
     
     
      option
     
     
      >
     
     
     
     
      <
     
     
      option
     
     
     
     
      v-else
     
     
     
     
      :value
     
     
      =
     
     
      "
     
     
      site.name
     
     
      "
     
     
      >
     
     
      {{site.name}}
     
     
      </
     
     
      option
     
     
      >
     
     
     
     
      </
     
     
      template
     
     
      >
     
     
     
     
      </
     
     
      select
     
     
      >
     
     
     
     
      <
     
     
      div
     
     
      >
     
     
      您选中了:{{selOption}}
     
     
      </
     
     
      div
     
     
      >
     
     
     
     
      </
     
     
      div
     
     
      >
     
     
     
     
      <
     
     
      script
     
     
      >
     
     
      const app = {
    data() {
        return {
            selOption: "Runoob",
            sites: [
                  {id:1,name:"Google"},
                  {id:2,name:"Runoob"},
                  {id:3,name:"Taobao"},
    methods:{
        changeVal:function(event){
            this.selOption = event.target.value;
            alert("你选中了"+this.selOption);
Vue.createApp(app).mount('#app')
     
     
      </
     
     
      script
     
     
      >
     
    
    
     尝试一下 »
    
    
     在组件上使用 v-for
    
    
     如果你还没了解组件的内容,可以先跳过这部分。
    
    
     在自定义组件上,你可以像在任何普通元素上一样使用 v-for:
    
    <my-component v-for="item in items" :key="item.id"></my-component>
    
     然而,任何数据都不会被自动传递到组件里,因为组件有自己独立的作用域。为了把迭代数据传递到组件里,我们要使用 props:
    
    <my-component
  v-for="(item, index) in items"
  :item="item"
  :index="index"
  :key="item.id"
></my-component>
    
     不自动将 item 注入到组件里的原因是,这会使得组件与 v-for 的运作紧密耦合。明确组件数据的来源能够使组件在其他场合重复使用。
    
    
     下面是一个简单的 todo 列表的完整例子:
    
    
     
      <
     
     
      div
     
     
     
     
      id
     
     
      =
     
     
      "
     
     
      todo-list-example
     
     
      "
     
     
      >
     
     
     
     
      <
     
     
      form
     
     
     
     
      v-on:submit
     
     
      .
     
     
      prevent
     
     
      =
     
     
      "
     
     
      addNewTodo
     
     
      "
     
     
      >
     
     
     
     
      <
     
     
      label
     
     
     
     
      for
     
     
      =
     
     
      "
     
     
      new-todo
     
     
      "
     
     
      >
     
     
      Add a todo
     
     
      </
     
     
      label
     
     
      >
     
     
     
     
      <
     
     
      input
     
     
     
     
      v-model
     
     
      =
     
     
      "
     
     
      newTodoText
     
     
      "
     
     
     
     
      id
     
     
      =
     
     
      "
     
     
      new-todo
     
     
      "
     
     
     
     
      placeholder
     
     
      =
     
     
      "
     
     
      E.g. Feed the cat
     
     
      "
     
     
     
     
      <
     
     
      button
     
     
      >
     
     
      Add
     
     
      </
     
     
      button
     
     
      >
     
     
     
     
      </
     
     
      form
     
     
      >
     
     
     
     
      <
     
     
      todo-item
     
     
     
     
      v-for
     
     
      =
     
     
      "
     
     
      (todo, index) in todos
     
     
      "
     
     
     
     
      :key
     
     
      =
     
     
      "
     
     
      todo.id
     
     
      "
     
     
     
     
      :title
     
     
      =
     
     
      "
     
     
      todo.title
     
     
      "
     
     
      @
     
     
      remove
     
     
      =
     
     
      "
     
     
      todos.splice(index, 1)
     
     
      "
     
     
     
     
      >
     
     
      </
     
     
      todo-item
     
     
      >
     
     
     
     
      </
     
     
      div
     
     
      >
     
    
    
     尝试一下 »