Blogger Information
Blog 42
fans 0
comment 0
visits 15684
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0804条件渲染与列表渲染
小言
Original
383 people have browsed it

列表渲染

在使用过程中,我们通常使用v-for指令来进行一组列表数组的渲染,在v-for中,我们拥有对父级作用域的完全访问权限。

  1. <ul>
  2. <li v-for="(city,index) of cities" :key="index">{{index}}:{{city}}</li>
  3. </ul>

条件渲染

v-if指令通常用于条件渲染,用true和false来表达。

  1. <div class="app">
  2. <p v-if="flag">
  3. {{message}}
  4. </p>
  5. <button @click="flag=!flag" v-text="flag ? '隐藏' : '显示'"></button>
  6. <!-- if-else, if else if else -->
  7. <p v-if="point >= 1000 && point < 2000">{{grade[0]}}</p>
  8. <p v-else-if="point >= 2000 && point < 3000">{{grade[1]}}</p>
  9. <p v-else-if="point >= 3000 && point < 4000">{{grade[2]}}</p>
  10. <p v-if="point >= 4000">{{grade[3]}}</p>
  11. <!-- <p v-else>{{grade[4]}}</p> -->
  12. </div>
  13. Vue.createApp({
  14. data() {
  15. return {
  16. message: '今天是前端最后一节课',
  17. flag:true,
  18. grade:['纸片会员', '木头会员', '铁皮会员', '金牌会员', '非会员'],
  19. //积分
  20. point:2500,
  21. };
  22. },
  23. }).mount('.app');

键盘修饰符

  1. <div class="app">
  2. <input type="text" @keydown.enter="submit($event)">
  3. <ul>
  4. <li v-for="(item,index) of list" :key="index">{{item}}</li>
  5. </ul>
  6. </div>
  7. <script>
  8. Vue.createApp({
  9. data(){
  10. return{
  11. list: [],
  12. };
  13. },
  14. methods:{
  15. submit(ev){
  16. if (ev.key === 'Enter'){
  17. this.list.unshift(ev.currentTarget.value);
  18. ev.currentTarget.value = null;
  19. } */
  20. this.list.unshift(ev.currentTarget.value);
  21. ev.currentTarget.value = null;
  22. },
  23. },
  24. }).mount('.app');
  25. </script>

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post