HtTabsComponentHref.vue
5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<template>
<div class="tab-container">
<el-tabs
class="fullheight tab-container-root"
v-model="currentTabComponent"
:type="finalTabsStyle.tabType"
@tab-click="handlePaneClick"
v-if="tabComponentOptions.length > 0"
>
<el-tab-pane
:label="tabComponent.name"
:name="tabComponent.alias"
v-for="(tabComponent, index) in tabComponentOptions"
:key="index"
>
<span slot="label">
<span class="ht-menu_title">
<span>{{tabComponent.name}}
</span>
<bookmark-star :tabAlias="tabComponent.alias"></bookmark-star>
</span>
</span>
<iframe
v-if="currentTabComponent == tabComponent.alias"
:src="tabComponent.href"
style="height:100%;width:100%;border:0px"
></iframe>
</el-tab-pane>
</el-tabs>
<iframe
v-if="tabComponentOptions.length <= 0"
:src="href ||currentHref"
style="height:100%;width:100%;border:0px"
></iframe>
</div>
</template>
<script>
import { mapGetters, mapState } from "vuex";
import menuApi from "@/api/menu.js";
import BookmarkStar from '@/components/common/BookmarkStar.vue';
export default {
name: "ht-tabs-component-href",
components: {BookmarkStar},
props: {},
computed: {
...mapState('menu',['menus']),
...mapGetters({
tabsStyle: "styleSetting/tabsStyle"
}),
finalTabsStyle: function() {
// 使用菜单的配置
if (
(this.tabsStyle.tabType == "menuSetting" ||
this.tabsStyle.tabType == null) &&
this.menuTabsStyle
) {
return this.menuTabsStyle;
}
// 使用默认的
if (
this.tabsStyle.tabType == null ||
((this.tabsStyle.tabType == "menuSetting" ||
this.tabsStyle.tabType == null) &&
!this.menuTabsStyle)
) {
return { tabType: "" };
}
// 使用全局的配置
return this.tabsStyle;
}
},
data() {
return {
href: "", //外部菜单url地址
tabComponentOptions: [],
currentTabComponent: "",
menuTabsStyle: null,
currentHref:''
};
},
computed: {
...mapState('menu',['selectedMenu'])
},
watch: {
'$route':{
handler(val){
if(val&&this.selectedMenu&&this.selectedMenu.href){
this.currentHref = this.selectedMenu.href
}
},
immediate:true,
deep:true
}
},
methods: {
handlePaneClick: function(data) {
if (data.alias) {
this.currentTabComponent = data.alias;
}
}
},
beforeRouteEnter(to, from, next) {
next(vm => {
// 通过 `vm` 访问组件实例
let routePath = vm.$route.path;
if (routePath) {
routePath = routePath.replace("/", "");
}
menuApi.getMenuByAlias(routePath, m => {
if (m) {
vm.$emit("menuChange", m);
if (m.children && m.children.length > 0) {
vm.tabComponentOptions = m.children;
vm.href = vm.tabComponentOptions[0].href;
vm.currentTabComponent =
m.activeTab || vm.tabComponentOptions[0].alias;
} else {
vm.tabComponentOptions = [];
vm.currentTabComponent = m.alias;
if (m.href.indexOf("?token=") == -1) {
vm.href =
m.href + "?token=" + vm.$store.state.login.currentUser.token;
} else {
vm.href = m.href;
}
}
if (m.tabsStyle) {
vm.menuTabsStyle = JSON.parse(m.tabsStyle);
} else {
vm.menuTabsStyle = null;
}
} else {
// 没有菜单的路由跳转 需要显示TAb名称需要定义tobName参数传递过来
vm.tabComponentOptions = [];
vm.currentTabComponent = to.name;
vm.$emit("menuChange", { name: to.params.tobName });
}
});
});
},
beforeRouteUpdate(to, from, next) {
// 在当前路由改变,但是该组件被复用时调用
// 举例来说,对于一个带有动态参数的路径 /foo/:id,在 /foo/1 和 /foo/2 之间跳转的时候,
// 由于会渲染同样的 Foo 组件,因此组件实例会被复用。而这个钩子就会在这个情况下被调用。
// 可以访问组件实例 `this`
// 通过 `vm` 访问组件实例
next();
const vm = this;
let routePath = to.fullPath;
if (routePath) {
routePath = routePath.replace("/", "");
}
menuApi.getMenuByAlias(routePath, m => {
if (m) {
vm.$emit("menuChange", m);
if (m.children && m.children.length > 0) {
vm.tabComponentOptions = m.children;
vm.href = vm.tabComponentOptions[0].href;
vm.currentTabComponent =
m.activeTab || vm.tabComponentOptions[0].alias;
} else {
vm.tabComponentOptions = [];
vm.currentTabComponent = m.alias;
if (m.href.indexOf("?token=") == -1) {
vm.href =
m.href + "?token=" + vm.$store.state.login.currentUser.token;
} else {
vm.href = m.href;
}
}
if (m.tabsStyle) {
vm.menuTabsStyle = JSON.parse(m.tabsStyle);
} else {
vm.menuTabsStyle = null;
}
} else {
// 没有菜单的路由跳转 需要显示TAb名称需要定义tobName参数传递过来
vm.tabComponentOptions = [];
vm.currentTabComponent = to.name;
vm.$emit("menuChange", { name: to.params.tobName });
}
});
}
};
</script>
<style lang="scss" scoped>
@import "@/assets/css/element-variables.scss";
.tab-container >>> .is-active .el-link {
color: $--color-primary;
font-weight: bold;
}
.tab-container > div.tab-container-root /deep/ > .el-tabs__content {
height: calc(100% - 60px);
}
.tab-container .el-tab-pane {
height: 100%;
}
</style>