vue中v-for和v-if一起使用之使用compute的示例代码

 更新时间:2022年05月07日 14:51:49   作者:不吃浅水鱼  
这篇文章主要介绍了vue中v-for和v-if一起使用之使用compute的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

版本

vue 2.9.6
element-ui: 2.15.6

目标效果

说明

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

解决方法

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

核心代码片段

# html
...
<el-divider content-position="left">奥迪</el-divider>
<el-link type="primary" target="_blank" v-for="(item, index) in links0" :key="index" :href="item.url" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >
  <el-button type="primary">{{item.name}}</el-button>
</el-link>
...
# script
...
computed: {
  links0: function() {
    return this.links.filter((item) => {
      return item.category === 0
    })
  },
  links1: function() {
    return this.links.filter((item) => {
      return item.category === 1
    })
  },
  links2: function() {
    return this.links.filter((item) => {
      return item.category === 2
    })
  }
},

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>
          <el-link type="primary" target="_blank" v-for="(item, index) in links0" :key="index" :href="item.url" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >
            <el-button type="primary">{{item.name}}</el-button>
          </el-link>
          <el-divider content-position="left">奔驰</el-divider>
          <el-link type="primary" target="_blank" v-for="(item, index) in links1" :key="index" :href="item.url" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >
            <el-button type="primary">{{item.name}}</el-button>
          </el-link>
          <el-divider content-position="left">宝马</el-divider>
          <el-link type="primary" target="_blank" v-for="(item, index) in links2" :key="index" :href="item.url" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >
            <el-button type="primary">{{item.name}}</el-button>
          </el-link>
        </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' }]
      },
    }
  },
  computed: {
    // 通过根据category属性来不同显示
    links0: function() {
      return this.links.filter((item) => {
        return item.category === 0
      })
    },
    links1: function() {
      return this.links.filter((item) => {
        return item.category === 1
      })
    },
    links2: function() {
      return this.links.filter((item) => {
        return item.category === 2
      })
    }
  },
  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一起使用之使用compute的文章就介绍到这了,更多相关vue v-for和v-if一起使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue3触发父组件两种写法

    vue3触发父组件两种写法

    这篇文章主要介绍了vue3触发父组件两种写法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-08-08
  • vue安装和使用scss及sass与scss的区别详解

    vue安装和使用scss及sass与scss的区别详解

    这篇文章主要介绍了vue安装和使用教程,用了很久css预编译器,但是一直不太清楚到底用的sass还是scss,直到有天被问住了有点尴尬,感兴趣的朋友一起看看吧
    2018-10-10
  • vue elementUI select下拉框如何设置默认值

    vue elementUI select下拉框如何设置默认值

    这篇文章主要介绍了vue elementUI select下拉框如何设置默认值问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10
  • Vue中computed和watch有哪些区别

    Vue中computed和watch有哪些区别

    这篇文章主要介绍了Vue中computed和watch有哪些区别,帮助大家更好的理解和使用vue框架,感兴趣的朋友可以了解下
    2020-12-12
  • 利用webstrom调试Vue.js单页面程序的方法教程

    利用webstrom调试Vue.js单页面程序的方法教程

    这篇文章主要给大家介绍了利用webstrom调试Vue.js单页面程序的方法教程,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编一起来学习学习吧。
    2017-06-06
  • vue组件开发之slider组件使用详解

    vue组件开发之slider组件使用详解

    这篇文章主要为大家详细介绍了vue组件开发之slider组件的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-08-08
  • vue中使用window.open()参数示例详解

    vue中使用window.open()参数示例详解

    这篇文章主要介绍了vue中使用window.open()参数详解,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-04-04
  • 详解Vue中如何进行分布式日志管理与日志分析

    详解Vue中如何进行分布式日志管理与日志分析

    在现代应用程序中,日志是一项重要的功能,用于帮助开发人员和运维人员了解应用程序的行为并进行故障排除,本文将介绍如何在Vue应用程序中实现分布式日志管理和日志分析功能,感兴趣的可以了解一下
    2023-06-06
  • 如何在vue中使用ant-design-vue组件

    如何在vue中使用ant-design-vue组件

    这篇文章主要介绍了如何在vue中使用ant-design-vue组件,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-01-01
  • Javascript vue.js表格分页,ajax异步加载数据

    Javascript vue.js表格分页,ajax异步加载数据

    这篇文章主要介绍了Javascript vue.js表格分页,ajax异步加载数据的相关资料,需要的朋友可以参考下
    2016-10-10

最新评论