詳解vue中v-for和v-if一起使用的替代方法template
vue中v-for和v-if一起使用的替代方法template
版本
vue 2.9.6
element-ui: 2.15.6
目標(biāo)效果

說明
在 vue 2.x 中,在一個(gè)元素上同時(shí)使用 v-if 和 v-for 時(shí),v-for 會(huì)優(yōu)先作用
解決方法
- 選擇性地渲染列表,例如根據(jù)某個(gè)特定屬性(
category)來決定不同展示渲染,使用計(jì)算屬性computed 見http://m.fzitv.net/article/247179.htm - 使用
template占位,將循環(huán)放在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="請(qǐng)輸入鏈接名稱"></el-input>
</el-form-item>
<el-form-item label="鏈接地址" prop="url">
<el-input size="small" v-model="editForm.url" auto-complete="off" placeholder="請(qǐng)輸入鏈接地址"></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表單驗(yàn)證
rules: {
name: [{ required: true, message: '請(qǐng)輸入鏈接名稱', trigger: 'blur' }],
url: [{ required: true, message: '請(qǐng)輸入鏈接地址', trigger: 'blur' }]
},
}
},
created() {
// 獲取鏈接
this.getdata()
},
// 這下面的方法只有被調(diào)用才會(huì)被執(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('鏈接添加失敗,請(qǐng)稍后再試')
if (error.response.status == 400 ) {
this.$message.error(error.response.data['url'][0]+'如: http://xxx 或 https://xxx')
}else {
this.$message.error('鏈接添加失敗,請(qǐng)稍后再試')
}
})
}
})
},
// 關(guān)閉添加鏈接窗口
closeDialog() {
this.editFormVisible = false
}
}
}
</script>
<style>
/* .el-row {
margin-top: 10px;
} */
.el-link {
margin-right: 5px;
}
</style>到此這篇關(guān)于詳解vue中v-for和v-if一起使用的替代方法template的文章就介紹到這了,更多相關(guān)vue 替代方法template內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue中v-for和v-if不能在同一個(gè)標(biāo)簽使用的最新解決方案
- 在vue+element-plus中無法同時(shí)使用v-for和v-if的問題及解決方法
- Vue3中v-if和v-for優(yōu)先級(jí)實(shí)例詳解
- Vue中判斷語句與循環(huán)語句基礎(chǔ)用法及v-if和v-for的注意事項(xiàng)詳解
- 詳解為什么Vue中的v-if和v-for不建議一起用
- vue的注意規(guī)范之v-if 與 v-for 一起使用教程
- Vue.js常用指令匯總(v-if、v-for等)
- vue中v-if和v-for一起使用的弊端及解決辦法(同時(shí)使用 v-if 和 v-for不推薦)
相關(guān)文章
vue+ts實(shí)現(xiàn)元素鼠標(biāo)拖動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了vue+ts實(shí)現(xiàn)元素鼠標(biāo)拖動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
electron-vite工具打包后如何通過內(nèi)置配置文件動(dòng)態(tài)修改接口地址
使用electron-vite?工具開發(fā)項(xiàng)目打包完后每次要改接口地址都要重新打包,對(duì)于多環(huán)境切換或者頻繁變更接口地址就顯得麻煩,這篇文章主要介紹了electron-vite工具打包后通過內(nèi)置配置文件動(dòng)態(tài)修改接口地址實(shí)現(xiàn)方法,需要的朋友可以參考下2024-05-05
vue3如何使用provide實(shí)現(xiàn)狀態(tài)管理詳解
Vue3中有一對(duì)新增的api,provide和inject,熟悉Vue2的朋友應(yīng)該明,這篇文章主要給大家介紹了關(guān)于vue3如何使用provide實(shí)現(xiàn)狀態(tài)管理的相關(guān)資料,需要的朋友可以參考下2021-10-10
vue使用ECharts實(shí)現(xiàn)折線圖和餅圖
這篇文章主要為大家詳細(xì)介紹了vue使用ECharts實(shí)現(xiàn)折線圖和餅圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
關(guān)于Vue中的計(jì)算屬性和監(jiān)聽屬性詳解
這篇文章主要介紹了關(guān)于Vue中的計(jì)算屬性和監(jiān)聽屬性詳解,Vue.js模板內(nèi)的表達(dá)式非常便利,但是設(shè)計(jì)它們的初衷是用于簡單運(yùn)算的,在模板內(nèi)放入過長的或復(fù)雜的邏輯時(shí),會(huì)讓模板過重且難以維護(hù),需要的朋友可以參考下2023-05-05
Vue Router的手寫實(shí)現(xiàn)方法實(shí)現(xiàn)
這篇文章主要介紹了Vue Router的手寫實(shí)現(xiàn)方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
淺談一下Vue生命周期中mounted和created的區(qū)別
每一個(gè)vue實(shí)例從創(chuàng)建到銷毀的過程,就是這個(gè)vue實(shí)例的生命周期,在這個(gè)過程中,他經(jīng)歷了從開始創(chuàng)建、初始化數(shù)據(jù)、編譯模板、掛載Dom、渲染→更新→渲染、卸載等一系列過程,那么這些過程中,具體vue做了些啥,我們今天來了解一下2023-05-05

