12
12
@click.middle.native =" closeSelectedTag(tag)"
13
13
@contextmenu.prevent.native =" openMenu(tag,$event)" >
14
14
{{ generateTitle(tag.title) }}
15
- <span class =" el-icon-close" @click.prevent.stop =" closeSelectedTag(tag)" />
15
+ <span v-if = " !tag.meta.affix " class =" el-icon-close" @click.prevent.stop =" closeSelectedTag(tag)" />
16
16
</router-link >
17
17
</scroll-pane >
18
18
<ul v-show =" visible" :style =" {left:left+'px',top:top+'px'}" class =" contextmenu" >
19
19
<li @click =" refreshSelectedTag(selectedTag)" >{{ $t('tagsView.refresh') }}</li >
20
- <li @click =" closeSelectedTag(selectedTag)" >{{ $t('tagsView.close') }}</li >
20
+ <li v-if =" !(selectedTag.meta&&selectedTag.meta.affix)" @click =" closeSelectedTag(selectedTag)" >{{
21
+ $t('tagsView.close') }}</li >
21
22
<li @click =" closeOthersTags" >{{ $t('tagsView.closeOthers') }}</li >
22
- <li @click =" closeAllTags" >{{ $t('tagsView.closeAll') }}</li >
23
+ <li @click =" closeAllTags(selectedTag) " >{{ $t('tagsView.closeAll') }}</li >
23
24
</ul >
24
25
</div >
25
26
</template >
26
27
27
28
<script >
28
29
import ScrollPane from ' @/components/ScrollPane'
29
30
import { generateTitle } from ' @/utils/i18n'
31
+ import path from ' path'
30
32
31
33
export default {
32
34
components: { ScrollPane },
@@ -35,17 +37,21 @@ export default {
35
37
visible: false ,
36
38
top: 0 ,
37
39
left: 0 ,
38
- selectedTag: {}
40
+ selectedTag: {},
41
+ affixTags: []
39
42
}
40
43
},
41
44
computed: {
42
45
visitedViews () {
43
46
return this .$store .state .tagsView .visitedViews
47
+ },
48
+ routers () {
49
+ return this .$store .state .permission .routers
44
50
}
45
51
},
46
52
watch: {
47
53
$route () {
48
- this .addViewTags ()
54
+ this .addTags ()
49
55
this .moveToCurrentTag ()
50
56
},
51
57
visible (value ) {
@@ -57,14 +63,44 @@ export default {
57
63
}
58
64
},
59
65
mounted () {
60
- this .addViewTags ()
66
+ this .initTags ()
67
+ this .addTags ()
61
68
},
62
69
methods: {
63
70
generateTitle, // generateTitle by vue-i18n
64
71
isActive (route ) {
65
72
return route .path === this .$route .path
66
73
},
67
- addViewTags () {
74
+ filterAffixTags (routes , basePath = ' /' ) {
75
+ let tags = []
76
+ routes .forEach (route => {
77
+ if (route .meta && route .meta .affix ) {
78
+ tags .push ({
79
+ path: path .resolve (basePath, route .path ),
80
+ name: route .name ,
81
+ meta: { ... route .meta }
82
+ })
83
+ }
84
+ if (route .children ) {
85
+ const tempTags = this .filterAffixTags (route .children , route .path )
86
+ if (tempTags .length >= 1 ) {
87
+ tags = [... tags, ... tempTags]
88
+ }
89
+ }
90
+ })
91
+
92
+ return tags
93
+ },
94
+ initTags () {
95
+ const affixTags = this .affixTags = this .filterAffixTags (this .routers )
96
+ for (const tag of affixTags) {
97
+ // Must have tag name
98
+ if (tag .name ) {
99
+ this .$store .dispatch (' addVisitedView' , tag)
100
+ }
101
+ }
102
+ },
103
+ addTags () {
68
104
const { name } = this .$route
69
105
if (name) {
70
106
this .$store .dispatch (' addView' , this .$route )
@@ -101,12 +137,7 @@ export default {
101
137
closeSelectedTag (view ) {
102
138
this .$store .dispatch (' delView' , view).then (({ visitedViews }) => {
103
139
if (this .isActive (view)) {
104
- const latestView = visitedViews .slice (- 1 )[0 ]
105
- if (latestView) {
106
- this .$router .push (latestView)
107
- } else {
108
- this .$router .push (' /' )
109
- }
140
+ this .toLastView (visitedViews)
110
141
}
111
142
})
112
143
},
@@ -116,9 +147,22 @@ export default {
116
147
this .moveToCurrentTag ()
117
148
})
118
149
},
119
- closeAllTags () {
120
- this .$store .dispatch (' delAllViews' )
121
- this .$router .push (' /' )
150
+ closeAllTags (view ) {
151
+ this .$store .dispatch (' delAllViews' ).then (({ visitedViews }) => {
152
+ if (this .affixTags .some (tag => tag .path === view .path )) {
153
+ return
154
+ }
155
+ this .toLastView (visitedViews)
156
+ })
157
+ },
158
+ toLastView (visitedViews ) {
159
+ const latestView = visitedViews .slice (- 1 )[0 ]
160
+ if (latestView) {
161
+ this .$router .push (latestView)
162
+ } else {
163
+ // You can set another route
164
+ this .$router .push (' /' )
165
+ }
122
166
},
123
167
openMenu (tag , e ) {
124
168
const menuMinWidth = 105
0 commit comments