在使用 Element UI 的 Tabs 組件時,動態添加 Vue 組件或頁面可以通過操作?tabs
?數組來實現。假設你要根據參數值來動態添加一個 Vue 頁面(這里假設是一個 Vue 組件),你可以按照以下步驟操作:
首先,確保你已經安裝并引入了 Element UI。
然后,在你的 Vue 組件中,定義一個數組來存儲 Tabs 的數據,每個 Tab 對象可以包含一個?name
?屬性和?content
?屬性等,content
?屬性可以用來存儲對應的 Vue 組件。
<template>
? <div>
??? <el-tabs v-model="activeTab" type="card">
????? <el-tab-pane
??????? v-for="(tab, index) in tabs"
??????? :key="index"
??????? :label="tab.name"
??????? :name="tab.name"
????? >
??????? <component :is="tab.content"></component>
????? </el-tab-pane>
??? </el-tabs>
? </div>
</template>
<script>
import ComponentA from './ComponentA.vue';
import ComponentB from './ComponentB.vue';
export default {
? data() {
??? return {
????? activeTab: 'tab1',
????? tabs: [
??????? {
????????? name: 'Tab 1',
????????? content: ComponentA
??????? }
????? ]
??? };
? },
? methods: {
??? addTab(param) {
????? // 假設 param 決定了要添加哪個組件
????? let newComponent;
????? if (param === 'a') {
??????? newComponent = ComponentA;
????? } else if (param === 'b') {
??????? newComponent = ComponentB;
????? }
????? if (newComponent) {
??????? const newTab = {
????????? name: `Tab ${this.tabs.length + 1}`,
????????? content: newComponent
??????? };
??????? this.tabs.push(newTab);
??????? this.activeTab = newTab.name; // 將新添加的 Tab 設置為激活狀態
????? }
??? }
? }
};
</script>
在這個例子中,addTab
?方法根據傳入的?param
?參數來決定添加哪個組件到 Tabs 中。你可以根據實際需要擴展?addTab
?方法,比如從服務器獲取組件名稱或根據其他條件動態決定添加的組件。