最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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 會優(yōu)先作用

解決方法

選擇性地渲染列表,例如根據(jù)某個特定屬性(category )來決定不同展示渲染,使用計算屬性computed使用template占位,將循環(huán)放在template中,v-if作用于元素,此方法script中不用定義computed方法,見 http://m.fzitv.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: {
    // 通過根據(jù)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()
  },
  // 這下面的方法只有被調(diào)用才會被執(zhí)行
  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一起使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • vue3觸發(fā)父組件兩種寫法

    vue3觸發(fā)父組件兩種寫法

    這篇文章主要介紹了vue3觸發(fā)父組件兩種寫法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • vue安裝和使用scss及sass與scss的區(qū)別詳解

    vue安裝和使用scss及sass與scss的區(qū)別詳解

    這篇文章主要介紹了vue安裝和使用教程,用了很久css預編譯器,但是一直不太清楚到底用的sass還是scss,直到有天被問住了有點尷尬,感興趣的朋友一起看看吧
    2018-10-10
  • vue elementUI select下拉框如何設置默認值

    vue elementUI select下拉框如何設置默認值

    這篇文章主要介紹了vue elementUI select下拉框如何設置默認值問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Vue中computed和watch有哪些區(qū)別

    Vue中computed和watch有哪些區(qū)別

    這篇文章主要介紹了Vue中computed和watch有哪些區(qū)別,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
    2020-12-12
  • 利用webstrom調(diào)試Vue.js單頁面程序的方法教程

    利用webstrom調(diào)試Vue.js單頁面程序的方法教程

    這篇文章主要給大家介紹了利用webstrom調(diào)試Vue.js單頁面程序的方法教程,文中介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面跟著小編一起來學習學習吧。
    2017-06-06
  • vue組件開發(fā)之slider組件使用詳解

    vue組件開發(fā)之slider組件使用詳解

    這篇文章主要為大家詳細介紹了vue組件開發(fā)之slider組件的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • vue中使用window.open()參數(shù)示例詳解

    vue中使用window.open()參數(shù)示例詳解

    這篇文章主要介紹了vue中使用window.open()參數(shù)詳解,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • 詳解Vue中如何進行分布式日志管理與日志分析

    詳解Vue中如何進行分布式日志管理與日志分析

    在現(xiàn)代應用程序中,日志是一項重要的功能,用于幫助開發(fā)人員和運維人員了解應用程序的行為并進行故障排除,本文將介紹如何在Vue應用程序中實現(xiàn)分布式日志管理和日志分析功能,感興趣的可以了解一下
    2023-06-06
  • 如何在vue中使用ant-design-vue組件

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

    這篇文章主要介紹了如何在vue中使用ant-design-vue組件,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-01-01
  • Javascript vue.js表格分頁,ajax異步加載數(shù)據(jù)

    Javascript vue.js表格分頁,ajax異步加載數(shù)據(jù)

    這篇文章主要介紹了Javascript vue.js表格分頁,ajax異步加載數(shù)據(jù)的相關資料,需要的朋友可以參考下
    2016-10-10

最新評論

游戏| 东阿县| 婺源县| 绥中县| 苏州市| 航空| 自贡市| 邓州市| 和静县| 红原县| 灌云县| 大姚县| 大洼县| 湖北省| 邯郸市| 永新县| 贵阳市| 门头沟区| 东安县| 九龙坡区| 饶河县| 清流县| 登封市| 南丹县| 应城市| 惠来县| 阿拉善盟| 平和县| 桦甸市| 张北县| 吉安市| 锡林郭勒盟| 蒙阴县| 南平市| 葫芦岛市| 南澳县| 青神县| 呈贡县| 浮梁县| 灵寿县| 长寿区|