详解vue中v-for和v-if一起使用的替代方法template

 更新时间:2022年05月07日 14:52:37   作者:不吃浅水鱼  
这篇文章主要介绍了vue中v-for和v-if一起使用的替代方法template,使用的版本是vue 2.9.6和element-ui: 2.15.6,通过实例代码给大家讲解的非常详细,需要的朋友可以参考下

vue中v-for和v-if一起使用的替代方法template

版本

vue 2.9.6
element-ui: 2.15.6

目标效果

说明

在 vue 2.x 中,在一个元素上同时使用 v-if 和 v-for 时,v-for 会优先作用

解决方法

  1. 选择性地渲染列表,例如根据某个特定属性(category )来决定不同展示渲染,使用计算属性computed 见https://www.jb51.net/article/247179.htm
  2. 使用template占位,将循环放在template中,v-if作用于元素,此方法script中不用定义computed方法

核心代码片段

<div>
  <el-divider content-position="left">奥迪</el-divider>
  <template v-for="(item, index) in links">
    <el-link type="primary" target="_blank" :href="item.url" :key="index" v-if="item.category==0">
      <el-button type="primary">{{item.name}}</el-button>
    </el-link>
  </template>
  <el-divider content-position="left">奔驰</el-divider>
  <template v-for="(item, index) in links">
    <el-link type="primary" target="_blank" :href="item.url" :key="index" v-if="item.category==1">
      <el-button type="primary">{{item.name}}</el-button>
    </el-link>
  </template>
  <el-divider content-position="left">宝马</el-divider>
  <template v-for="(item, index) in links">
    <el-link type="primary" target="_blank" :href="item.url" :key="index" v-if="item.category==2">
      <el-button type="primary">{{item.name}}</el-button>
    </el-link>
  </template>
</div>

Car.vue

<template>
  <div>
    <el-row>
      <el-col :span="24">
        <el-form :inline="true" class="user-search">
          <el-form-item>
            <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()" plain>添加链接</el-button>
          </el-form-item>
        </el-form>
        <div>
          <el-divider content-position="left">奥迪</el-divider>
          <template v-for="(item, index) in links">
            <el-link type="primary" target="_blank" :href="item.url" :key="index" v-if="item.category==0">
              <el-button type="primary">{{item.name}}</el-button>
            </el-link>
          </template>
          <el-divider content-position="left">奔驰</el-divider>
          <template v-for="(item, index) in links">
            <el-link type="primary" target="_blank" :href="item.url" :key="index" v-if="item.category==1">
              <el-button type="primary">{{item.name}}</el-button>
            </el-link>
          </template>
          <el-divider content-position="left">宝马</el-divider>
          <template v-for="(item, index) in links">
            <el-link type="primary" target="_blank" :href="item.url" :key="index" v-if="item.category==2">
              <el-button type="primary">{{item.name}}</el-button>
            </el-link>
          </template>
        </div>
        <!-- 添加界面 -->
        <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
          <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
          <el-form-item label="链接名称" prop="name">
            <el-input size="small" v-model="editForm.name" auto-complete="off" placeholder="请输入链接名称"></el-input>
          </el-form-item>
          <el-form-item label="链接地址" prop="url">
            <el-input size="small" v-model="editForm.url" auto-complete="off" placeholder="请输入链接地址"></el-input>
          </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
          <el-button size="small" @click="closeDialog">取消</el-button>
          <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存</el-button>
          </div>
        </el-dialog>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { getLink, saveLink } from '../../api/userMG'
export default {
  data() {
    return {
      links: [],
      loading: false, //显示加载
      editFormVisible: false, //控制添加页面显示与隐藏
      title: '添加链接',
      editForm: {
        name: '',
        url: ''
      },
      // rules表单验证
      rules: {
        name: [{ required: true, message: '请输入链接名称', trigger: 'blur' }],
        url: [{ required: true, message: '请输入链接地址', trigger: 'blur' }]
      },
    }
  },
  created() {
    // 获取链接
   this.getdata()
  },
  // 这下面的方法只有被调用才会被执行
  methods: {
    // 获取链接
    getdata() {
      this.loading = true
      getLink().then((response) => {
        this.loading = false
        console.log(response.data)
        this.links = response.data
      }).catch(error => {
        console.log(error)
      })
    },
    // 添加页面方法
    handleEdit: function() {
      this.editFormVisible = true
      this.title = '添加链接',
      this.editForm.name = '',
      this.editForm.url = ''
    },
    // 添加保存页面方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          saveLink(this.editForm).then(response => {
            this.editFormVisible = false
            this.loading = false
            // if (response.success)
            this.getdata()
            this.$message({
              type: 'success',
              message: '链接添加成功'
            })
          }).catch(error => {
            this.editFormVisible = false
            this.loading = false
            // console.log(error.response.data['url'][0])
            // console.log(error.response.status)
            // this.$message.error('链接添加失败,请稍后再试')
            if (error.response.status == 400 ) {
              this.$message.error(error.response.data['url'][0]+'如: http://xxx 或 https://xxx')
            }else {
              this.$message.error('链接添加失败,请稍后再试')
            }
          })
        }
      })
    },
    // 关闭添加链接窗口
    closeDialog() {
      this.editFormVisible = false
    }
  }
}
</script>
<style>
  /* .el-row {
    margin-top: 10px;
  } */
  .el-link {
    margin-right: 5px;
  }
</style>

到此这篇关于详解vue中v-for和v-if一起使用的替代方法template的文章就介绍到这了,更多相关vue 替代方法template内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue+ts实现元素鼠标拖动效果

    vue+ts实现元素鼠标拖动效果

    这篇文章主要为大家详细介绍了vue+ts实现元素鼠标拖动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • electron-vite工具打包后如何通过内置配置文件动态修改接口地址

    electron-vite工具打包后如何通过内置配置文件动态修改接口地址

    使用electron-vite 工具开发项目打包完后每次要改接口地址都要重新打包,对于多环境切换或者频繁变更接口地址就显得麻烦,这篇文章主要介绍了electron-vite工具打包后通过内置配置文件动态修改接口地址实现方法,需要的朋友可以参考下
    2024-05-05
  • vue3如何使用provide实现状态管理详解

    vue3如何使用provide实现状态管理详解

    Vue3中有一对新增的api,provide和inject,熟悉Vue2的朋友应该明,这篇文章主要给大家介绍了关于vue3如何使用provide实现状态管理的相关资料,需要的朋友可以参考下
    2021-10-10
  • Vue Router路由守卫超详细介绍

    Vue Router路由守卫超详细介绍

    路由守卫,简单理解来说就是,当用户要进行一些操作时,我需要用户的一些信息或数据或行为,我判断过后,才会同意用户进行操作,说到这里,我想大家心里都或多或少有点理解了吧
    2023-01-01
  • uni-popup手写菜鸟上门取件时间选择器

    uni-popup手写菜鸟上门取件时间选择器

    这篇文章主要为大家介绍了uni-popup手撸了一个菜鸟上门取件时间选择器,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • vue使用ECharts实现折线图和饼图

    vue使用ECharts实现折线图和饼图

    这篇文章主要为大家详细介绍了vue使用ECharts实现折线图和饼图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • 关于Vue中的计算属性和监听属性详解

    关于Vue中的计算属性和监听属性详解

    这篇文章主要介绍了关于Vue中的计算属性和监听属性详解,Vue.js模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的,在模板内放入过长的或复杂的逻辑时,会让模板过重且难以维护,需要的朋友可以参考下
    2023-05-05
  • Vue Router的手写实现方法实现

    Vue Router的手写实现方法实现

    这篇文章主要介绍了Vue Router的手写实现方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • 详解Vue中的watch和computed

    详解Vue中的watch和computed

    这篇文章主要介绍了Vue中的watch和computed的相关资料,帮助大家更好的理解和学习vue框架,感兴趣的朋友可以了解下
    2020-11-11
  • 浅谈一下Vue生命周期中mounted和created的区别

    浅谈一下Vue生命周期中mounted和created的区别

    每一个vue实例从创建到销毁的过程,就是这个vue实例的生命周期,在这个过程中,他经历了从开始创建、初始化数据、编译模板、挂载Dom、渲染→更新→渲染、卸载等一系列过程,那么这些过程中,具体vue做了些啥,我们今天来了解一下
    2023-05-05

最新评论