Vue動(dòng)態(tài)組件表格的實(shí)現(xiàn)代碼
更新時(shí)間:2022年10月16日 11:51:47 作者:生活在北極的企鵝
這篇文章主要介紹了Vue動(dòng)態(tài)組件表格的實(shí)現(xiàn)代碼,包括框架結(jié)構(gòu)組件,文中還給大家封裝了幾個(gè)組件,有按鈕組件、圖片組件、滑動(dòng)開關(guān),結(jié)合示例代碼給大家詳細(xì)講解,需要的朋友可以參考下
Vue組件
數(shù)據(jù)源
//這里是HTML內(nèi)容 這里通過下面的引入框架結(jié)構(gòu)把數(shù)據(jù)源傳到框架中 還有匹配項(xiàng)
<Mytable :configList="configList" :configData="configData"></Mytable>
// 引入結(jié)構(gòu)組件
import myCard from "./components/card";
// 注冊(cè)組件
components: { myCard },
data() {
return {
// 這里定義數(shù)據(jù)列表
configList: [
// 這里是數(shù)據(jù)有源
{
text: "111",
img: "/02.jpg",
tap: "標(biāo)簽1",
switch: true,
button: "按鈕1",
},
],
// 這里定義匹配項(xiàng)
configData: [
{
table: "貨幣",
porp: "text",
name: "MyText",
},
{
table: "圖片",
porp: "img",
name: "Myimg",
},
{
table: "標(biāo)簽",
porp: "tap",
name: "tag",
},
{
table: "滑動(dòng)開關(guān)",
porp: "switch",
name: "Btn",
funName: (row) => {
this.mySwitch(row);
},
},
{
table: "按鈕",
porp: "button",
name: "Mybtn",
// 如果組件中需要?jiǎng)討B(tài)綁定事件 在這里設(shè)置
funName: (row) => {
this.myBtn(row);
},
},
]
}
]框架結(jié)構(gòu)組件
<div>
// 這里接受數(shù)據(jù)組件傳遞過來的數(shù)據(jù)
<el-table :data="configList">
<!-- 文字表格區(qū)間 -->
// 這里進(jìn)行循環(huán)渲染數(shù)據(jù)
<el-table-column
align="center"
v-for="(item, index) in configData"
:key="index"
:label="item.table"
>
<!-- 組件 -->
// 動(dòng)態(tài)組件 這里可以進(jìn)行標(biāo)簽 按鈕 圖片等 的別的組件進(jìn)行循環(huán)渲染到表格中
<template slot-scope="scope">
<component
:is="item.name"
:value="scope.row"
// 把每一項(xiàng)有點(diǎn)擊事件進(jìn)行傳參
@parentFun="fun(item.funName, scope.row)"
></component>
</template>
</el-table-column>
</el-table>
</div>
// 這里引用自己封裝的動(dòng)態(tài)組件
import Myimg from "@/components/toConfigure/img.vue";
import tag from "@/components/toConfigure/tag.vue";
import Btn from "@/components/toConfigure/switch.vue";
import MyText from "@/components/toConfigure/text.vue";
import Mybtn from "@/components/toConfigure/button.vue";
// 進(jìn)行注冊(cè)組件
components: {
Myimg,
tag,
Btn,
MyText,
Mybtn,
},
// 這里進(jìn)行判斷每個(gè)按鈕時(shí)候有點(diǎn)擊事件 沒有為空
methods: {
fun(funName, row) {
if (funName) {
funName(row);
}
},
},
// 這里接受傳過來的數(shù)據(jù)
props: {
configData: {
type: Array,
},
configList: {
type: Array,
},
},這里我自己封裝了幾個(gè)組件
按鈕組件
<template>
// 這里是按鈕
<el-button round @click="btn">{{ value.button }}</el-button>
</template>
<script>
export default {
// 接受組件傳過來的值
props: {
value: {
type: Object,
},
},
// 這里進(jìn)行綁定動(dòng)態(tài)點(diǎn)擊事件
methods: {
btn() {
// 這里接受傳參
this.$emit("parentFun");
},
},
};
</script>
<style></style>圖片組件
<template>
<div>
<el-image
style="width: 100px; height: 100px"
:src="Myimg"
// 使用時(shí)候把這條注釋刪除 這個(gè)屬性是點(diǎn)擊圖片放大 不需要可以刪除
:preview-src-list="[Myimg]"
></el-image>
</div>
</template>
<script>
export default {
props: {
value: {
type: Object,
},
},
computed: {
Myimg() {
if (this.value.img.length > 0) {
// "@/assets/images" 這個(gè)是圖片的根路徑 加上 傳遞過來的數(shù)據(jù)中圖片的名字
return require("@/assets/images" + this.value.img);
} else {
return;
}
},
},
};
</script>
<style></style>滑動(dòng)開關(guān)
<template>
<div>
<el-switch
v-if="this.value.switch !== undefined"
v-model="value.switch"
active-color="#13ce66"
inactive-color="#ff4949"
@change="switchClick"
></el-switch>
</div>
</template>
<script>
export default {
props: {
value: {
type: Object,
},
},
methods: {
switchClick() {
// 事件分發(fā)
this.$emit("parentFun", this.value);
},
},
mounted() {
// console.log(this.value.button);
},
};
</script>
<style></style>tap組件
<template>
<div>
<el-tag v-if="value.tap.length > 0">{{ value.tap }}</el-tag>
</div>
</template>
<script>
export default {
props: {
value: {
type: Object,
},
},
};
</script>
<style></style>text組件
<template>
<div>
{{ value.text }}
</div>
</template>
<script>
export default {
props: {
value: {
type: Object,
},
},
};
</script>
<style></style>到此這篇關(guān)于Vue動(dòng)態(tài)組件 表格的文章就介紹到這了,更多相關(guān)Vue動(dòng)態(tài)組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue?axios?form-data格式傳輸數(shù)據(jù)和文件方式
這篇文章主要介紹了vue?axios?form-data格式傳輸數(shù)據(jù)和文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
一文教會(huì)你搭建vite項(xiàng)目并配置路由和element-plus
由于項(xiàng)目搭建過程實(shí)在繁瑣,容易遺忘,每次新建項(xiàng)目還得百度一下怎么搭建,所以寫下本文提醒自己,下面這篇文章主要給大家介紹了關(guān)于搭建vite項(xiàng)目并配置路由和element-plus的相關(guān)資料,需要的朋友可以參考下2022-07-07
Vue計(jì)算屬性與監(jiān)視屬性詳細(xì)分析使用
computed是vue的配置選項(xiàng),它的值是一個(gè)對(duì)象,其中可定義多個(gè)計(jì)算屬性,每個(gè)計(jì)算屬性就是一個(gè)函數(shù),下面這篇文章主要給大家介紹了關(guān)于vue中計(jì)算屬性computed的詳細(xì)講解,需要的朋友可以參考下2022-11-11
如何使用Vuex+Vue.js構(gòu)建單頁應(yīng)用
這篇文章主要教大家如何使用Vuex+Vue.js構(gòu)建單頁應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
手把手教你創(chuàng)建vue3項(xiàng)目的最佳方式
如今的Vue3已經(jīng)勢(shì)不可擋,當(dāng)然搭建一個(gè)全新的Vue3項(xiàng)目也有了全新的方式,下面這篇文章主要給大家介紹了關(guān)于如何手把手教你創(chuàng)建vue3項(xiàng)目的最佳方式,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
徹底搞懂并解決vue-cli4中圖片顯示的問題實(shí)現(xiàn)
這篇文章主要介紹了徹底搞懂并解決vue-cli4中圖片顯示的問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

