Maison >interface Web >js tutoriel >vue.js指令v-for使用及索引获取

vue.js指令v-for使用及索引获取

高洛峰
高洛峰original
2016-12-08 09:43:552092parcourir

1.v-for

  直接上代码。

示例一:

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
 <title></title>
</head>
<body>
 <div id="didi-navigator">
 <ul>
  <li v-for="tab in tabs">
  {{ tab.text }}
  </li>
 </ul>
 </div>
 <script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
 <script type="text/javascript">
 new Vue({
  el: &#39;#didi-navigator&#39;,
  data: {
  tabs: [
   { text: &#39;巴士&#39; },
   { text: &#39;快车&#39; },
   { text: &#39;专车&#39; },
   { text: &#39;顺风车&#39; },
   { text: &#39;出租车&#39; },
   { text: &#39;代驾&#39; }
  ]
  }
 })
 </script>
</body>
</html>

   

2.索引

  在 v-for 块内我们能完全访问父组件作用域内的属性,另有一个特殊变量 $index,正如你猜到的,它是当前数组元素的索引:

<ul id="example-2">
 <li v-for="item in items">
 {{ parentMessage }} - {{ $index }} - {{ item.message }}
 </li>
</ul>

    

var example2 = new Vue({
 el: &#39;#example-2&#39;,
 data: {
 parentMessage: &#39;Parent&#39;,
 items: [
 { message: &#39;Foo&#39; },
 { message: &#39;Bar&#39; }
 ]
 }
})

   

  另外,你可以为索引指定一个别名(如果 v-for 用于一个对象,则可以为对象的键指定一个别名):

<div v-for="(index, item) in items">
 {{ index }} {{ item.message }}
</div>

   

   从 1.0.17 开始可以使用 of 分隔符,更接近 JavaScript 遍历器语法:

11039faa436139e63b4f4622625f097b16b28748ea4df4d9c2150843fecfba68

示例二:

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
 <title></title>
</head>
<body>
 <ul>
 <li v-for="option in options">
  <p class="text-success" v-on:click="getIndex($index)">Text:{{option.text}}--Vlue:{{option.value}}</p>
 </li>
 </ul>
 <div v-if="isNaN(click)==false">
 <span>你点击的索引为: {{ click }}</span>
 </div>
 <div v-else>
 <p class="text-danger">试着点击上方LI条目</p>
 </div>
 <script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
 <script type="text/javascript">
 new Vue({
  el: &#39;body&#39;,
  data: {
  click: &#39;a&#39;,
  options: [
   { text: &#39;上海市&#39;, value: &#39;20&#39; },
   { text: &#39;湖北省&#39;, value: &#39;43&#39; },
   { text: &#39;河南省&#39;, value: &#39;45&#39; },
   { text: &#39;北京市&#39;, value: &#39;10&#39; }
  ]
  },
  methods:{
  getIndex:function($index){
   this.click=$index;
  }
  }
 });
 </script>
</body>
</html>

   

3.在点击事件中取到索引

方法一:添加自定义属性

示例三:

<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8">
 <title></title>
 <style type="text/css">
  a{display: block;}
 </style>
 </head>
 <body>
 <div>
  <a v-for="(index,item) in items" data-index="{{index}}" v-on:click="onclick" href="http://www.baidu.com">{{ item.text }}</a>
 </div>
 <input type="text" name="" id="index" value=""/>
 <script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
 <script type="text/javascript">
 new Vue({
  el: &#39;body&#39;,
  data: {
  items: [
   { text: &#39;巴士&#39; },
   { text: &#39;快车&#39; },
   { text: &#39;专车&#39; },
   { text: &#39;顺风车&#39; },
   { text: &#39;出租车&#39; },
   { text: &#39;代驾&#39; }
  ]
  },
  methods: {
  onclick:function(event){
   event.preventDefault();
   let target = event.target
   console.log(target.getAttribute("data-index"));
   document.getElementById(&#39;index&#39;).value = target.getAttribute("data-index");
  }
  }
 })
 </script>
 </body>
</html>

   

方法二:直接传入索引值

示例四(和二差不多):

<!DOCTYPE html>
 
<html>
 
<head>
 
<meta charset="UTF-8">
 
<title></title>
 
<style type="text/css">
 
a{display: block;}
 
</style>
 
</head>
 
<body>
 
<div>
 
 <a v-for="(index,item) in items" v-on:click="onclick($index)" href="#">{{ item.text }}</a>
 
</div>
 
<input type="text" name="" id="index" value=""/>
 
<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
 
 <script type="text/javascript">
 
 new Vue({
 
 el: &#39;body&#39;,
 
 data: {
 
 items: [
 
 { text: &#39;巴士&#39; },
 
 { text: &#39;快车&#39; },
 
 { text: &#39;专车&#39; },
 
 { text: &#39;顺风车&#39; },
 
 { text: &#39;出租车&#39; },
 
 { text: &#39;代驾&#39; }
 
 ]
 
 },
 
 methods: {
 
 onclick:function(index){
 
// index.preventDefault();
 
 console.log(index);
 
 document.getElementById(&#39;index&#39;).value = index;
 
}
 
 }
 
})
 
</script>
 
</body>
 
</html>

   

  效果与方法一相同。

如果想直接传索引可以用以下方法:

示例五:

<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8">
 <title></title>
 <style type="text/css">
  a{display: block;}
 </style>
 </head>
 <body>
 <div>
  <a v-for="(index,item) in items" v-on:click="onclick($index)" href="javascript:void(0)">{{ item.text }}</a>
 </div>
 <input type="text" name="" id="index" value=""/>
 <script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
 <script type="text/javascript">
 new Vue({
  el: &#39;body&#39;,
  data: {
  items: [
   { text: &#39;巴士&#39; },
   { text: &#39;快车&#39; },
   { text: &#39;专车&#39; },
   { text: &#39;顺风车&#39; },
   { text: &#39;出租车&#39; },
   { text: &#39;代驾&#39; }
  ]
  },
  methods: {
  onclick:function(index){
//   index.preventDefault();
   console.log(index);
   document.getElementById(&#39;index&#39;).value = index;
   window.location.href = "http://www.baidu.com";
  }
  }
 })
 </script>
 </body>
</html>

   


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn