Skip to content

Commit d1a5f21

Browse files
committed
refactor(code): 代码规范,解决遗留的 TODO 列表项
1 parent ae9bedb commit d1a5f21

File tree

11 files changed

+54
-35
lines changed

11 files changed

+54
-35
lines changed

src/components/HeaderSearch/index.vue

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div :class="{'show':show}" class="header-search">
33
show {{show}}
4-
<svg-icon name="search" class="search-icon" @click="click"/>
4+
<svg-icon name="search" class="search-icon" @click="click" />
55
<el-select
66
ref="headerSearchSelect"
77
v-model="search"
@@ -12,27 +12,28 @@
1212
placeholder="Search"
1313
class="header-search-select"
1414
@change="change">
15-
<el-option v-for="item in options" :key="item.path" :value="item" :label="item.title.join(' > ')"/>
15+
<el-option v-for="item in options" :key="item.path" :value="item" :label="item.title.join(' > ')" />
1616
</el-select>
1717
</div>
1818
</template>
1919

2020
<script lang="ts">
2121
import { Component, Vue, Watch } from 'vue-property-decorator';
2222
import { ElSelect } from 'element-ui/types/select';
23+
import { RouteConfig } from 'vue-router';
2324
import Fuse from 'fuse.js';
2425
import path from 'path';
2526
import i18n from '@/lang';
2627
2728
@Component({
28-
name: 'Search'
29+
name: 'HeaderSearch'
2930
})
3031
export default class HeaderSearch extends Vue {
3132
search: string = '';
32-
options: any[] = [];
33-
searchPool: any[] = [];
33+
options: RouteConfig[] = [];
34+
searchPool: RouteConfig[] = [];
3435
show: boolean = false;
35-
fuse: any = null;
36+
fuse?: Fuse<RouteConfig>;
3637
3738
get routers() {
3839
return this.$store.getters.permission_routers;
@@ -59,14 +60,14 @@ export default class HeaderSearch extends Vue {
5960
6061
@Watch('show')
6162
onShowChange(value) {
62-
// TODO 这里 addEventListener 会被立刻执行,所以迁移到了created
63+
if (value) {
64+
document.body.addEventListener('click', this.close);
65+
} else {
66+
document.body.removeEventListener('click', this.close);
67+
}
6368
console.log('onShowChange value', value);
6469
}
6570
66-
created() {
67-
document.body.addEventListener('click', this.close);
68-
}
69-
7071
mounted() {
7172
this.searchPool = this.generateRouters(this.routers);
7273
}
@@ -130,16 +131,18 @@ export default class HeaderSearch extends Vue {
130131
continue;
131132
}
132133
133-
const data = {
134+
const data: RouteConfig = {
134135
path: path.resolve(basePath, router.path),
135-
title: [...prefixTitle]
136+
meta: {
137+
title: [...prefixTitle]
138+
}
136139
};
137140
138141
if (router.meta && router.meta.title) {
139142
// generate internationalized title
140143
const i18nTitle = i18n.t(`route.${router.meta.title}`);
141144
142-
data.title = [...data.title, i18nTitle];
145+
data.meta.title = [...data.meta.title, i18nTitle];
143146
144147
if (router.redirect !== 'noredirect') {
145148
// only push the routes with title
@@ -150,7 +153,7 @@ export default class HeaderSearch extends Vue {
150153
151154
// recursive child routers
152155
if (router.children) {
153-
const tempRouters = this.generateRouters(router.children, data.path, data.title);
156+
const tempRouters = this.generateRouters(router.children, data.path, data.meta.title);
154157
if (tempRouters.length >= 1) {
155158
res = [...res, ...tempRouters];
156159
}
@@ -161,7 +164,9 @@ export default class HeaderSearch extends Vue {
161164
162165
querySearch(query) {
163166
if (query !== '') {
164-
this.options = this.fuse.search(query);
167+
if (this.fuse) {
168+
this.options = this.fuse.search(query);
169+
}
165170
} else {
166171
this.options = [];
167172
}

src/components/LangSelect/index.vue

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,19 @@
1414
<script lang="ts">
1515
import { Component, Vue } from 'vue-property-decorator';
1616
import { AppModule } from '@/store/modules/app';
17-
import i18n from '@/lang';
18-
import {Message} from 'element-ui';
1917
20-
@Component
18+
@Component({
19+
name: 'LangSelect'
20+
})
2121
export default class LangSelect extends Vue {
2222
get lang() {
2323
return this.$store.getters.language;
2424
}
2525
2626
private handleSetLanguage(lang): void {
27-
// TODO this 指向为 null?
28-
// console.log('this', this);
29-
// console.log('lang', lang);
30-
i18n.locale = lang;
27+
this.$i18n.locale = lang;
3128
AppModule.SetLanguage(lang);
32-
Message({
29+
this.$message({
3330
message: 'Switch Language Success',
3431
type: 'success'
3532
});

src/layout/components/AppMain.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111
<script lang="ts">
1212
import { Component, Vue } from 'vue-property-decorator';
1313
14-
@Component
14+
@Component({
15+
name: 'AppMain'
16+
})
1517
export default class AppMain extends Vue {
1618
get cachedViews() {
1719
return this.$store.getters.cachedViews;
1820
}
1921
2022
get key() {
21-
return this.$route.fullPath;
23+
return this.$route.path;
2224
}
2325
}
2426
</script>

src/layout/components/Navbar.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import {
6363
} from '@/components';
6464
6565
@Component({
66+
name: 'Navbar',
6667
components: {
6768
Breadcrumb,
6869
Hamburger,

src/layout/components/Settings/index.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import { SettingsModule } from '@/store/modules/settings';
3434
3535
@Component({
36+
name: 'Settings',
3637
components: {
3738
ThemePicker
3839
}

src/layout/components/Sidebar/Link.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import { Component, Vue, Prop } from 'vue-property-decorator';
1010
import { isExternal } from '@/utils';
1111
12-
@Component
12+
@Component({
13+
name: 'Link'
14+
})
1315
export default class Link extends Vue {
1416
@Prop({required: true, default: ''}) to!: string;
1517

src/layout/components/Sidebar/Logo.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
<script lang="ts">
1717
import { Component, Vue, Prop } from 'vue-property-decorator';
1818
19-
@Component
19+
@Component({
20+
name: 'Logo'
21+
})
2022
export default class Logo extends Vue {
2123
@Prop({required: true}) collapse!: boolean;
2224
title: string = 'Vue Element Admin';

src/layout/components/Sidebar/SidebarItem.vue

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path)">
77
<el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
88
<template v-if="onlyOneChild.meta">
9-
<svg-icon v-if="onlyOneChild.meta.icon" :name="onlyOneChild.meta.icon || item.meta.icon"/>
9+
<svg-icon v-if="onlyOneChild.meta.icon" :name="onlyOneChild.meta.icon || item.meta.icon" />
1010
<span slot="title">{{generateTitle(onlyOneChild.meta.title)}}</span>
1111
</template>
1212
</el-menu-item>
@@ -15,7 +15,7 @@
1515

1616
<el-submenu v-else ref="subMenu" :index="resolvePath(item.path)" popper-append-to-body>
1717
<template slot="title">
18-
<svg-icon v-if="item.meta.icon" :name="item.meta.icon"/>
18+
<svg-icon v-if="item.meta.icon" :name="item.meta.icon" />
1919
<span slot="title">{{generateTitle(item.meta.title)}}</span>
2020
</template>
2121
<template v-for="child in item.children">
@@ -26,12 +26,12 @@
2626
:item="child"
2727
:key="child.path"
2828
:base-path="resolvePath(child.path)"
29-
class="nest-menu"/>
29+
class="nest-menu" />
3030

3131
<app-link v-else :to="resolvePath(child.path)" :key="child.name">
3232
<el-menu-item :index="resolvePath(child.path)">
3333
<template v-if="child.meta">
34-
<svg-icon v-if="child.meta.icon" :name="child.meta.icon"/>
34+
<svg-icon v-if="child.meta.icon" :name="child.meta.icon" />
3535
<span slot="title">{{generateTitle(child.meta.title)}}</span>
3636
</template>
3737
</el-menu-item>
@@ -44,20 +44,21 @@
4444

4545
<script lang="ts">
4646
import { Component, Vue, Prop } from 'vue-property-decorator';
47-
import { Route } from 'vue-router';
47+
import { RouteConfig } from 'vue-router';
4848
import path from 'path';
4949
import { isExternal } from '@/utils';
5050
import AppLink from './Link.vue';
5151
import FixiOSBug from './FixiOSBug';
5252
5353
@Component({
54+
name: 'SidebarItem',
5455
components: {
5556
AppLink
5657
},
5758
mixins: [FixiOSBug]
5859
})
5960
export default class SidebarItem extends Vue {
60-
@Prop({required: true, default: {}}) item!: Route;
61+
@Prop({required: true, default: {}}) item!: RouteConfig;
6162
@Prop({default: false}) isNest!: boolean;
6263
@Prop({default: ''}) basePath!: string;
6364

src/layout/components/Sidebar/index.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import variable from '@/assets/js/variable';
2525
2626
@Component({
27+
name: 'Sidebar',
2728
components: {
2829
SidebarItem,
2930
Logo

src/layout/components/TagsView/index.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import ScrollPane from './ScrollPane.vue';
3232
import path from 'path';
3333
3434
@Component({
35+
name: 'TagsView',
3536
components: {
3637
ScrollPane
3738
}

0 commit comments

Comments
 (0)