Skip to content

Commit bc77070

Browse files
committed
feat: migrate to TS, close #70
1 parent 454bd6f commit bc77070

File tree

5 files changed

+74
-64
lines changed

5 files changed

+74
-64
lines changed

src/components/breadcrumbs.vue

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/index.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/index.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import {ComponentOptions, PluginObject, VueConstructor} from 'vue'
2+
import { Route } from 'vue-router';
3+
4+
type CallbackFunction = () => string;
5+
6+
class VueBreadcrumbs implements PluginObject<ComponentOptions<Vue>> {
7+
install(Vue: VueConstructor<Vue>, options = {}) {
8+
9+
Object.defineProperties(Vue.prototype, {
10+
$breadcrumbs: {
11+
get(): Route[] {
12+
return this.$route.matched.map((route: Route) => ({
13+
...route,
14+
path: route.path.length > 0 ? route.path : '/'
15+
}));
16+
}
17+
}
18+
});
19+
20+
Vue.component('Breadcrumbs', Vue.extend({
21+
methods: {
22+
getBreadcrumb(bc: string | CallbackFunction): string {
23+
return typeof bc === 'function' ? bc.call(this, this.$route.params) : bc;
24+
},
25+
getPath(crumb: Route): string {
26+
let {path} = crumb;
27+
28+
for (const [key, value] of Object.entries(this.$route.params)) {
29+
path = path.replace(`:${key}`, value);
30+
}
31+
32+
return path;
33+
}
34+
},
35+
template: `
36+
<ol
37+
class="breadcrumb"
38+
v-if="$breadcrumbs.length"
39+
>
40+
<li
41+
class="breadcrumb-item"
42+
v-if="crumb.meta.breadcrumb"
43+
v-for="(crumb, i) in $breadcrumbs"
44+
:key="i"
45+
>
46+
<router-link
47+
active-class="active"
48+
:to="{ path: getPath(crumb) }"
49+
:tag="i != $breadcrumbs.length - 1 ? 'a' : 'span'"
50+
>
51+
{{ getBreadcrumb(crumb.meta.breadcrumb) }}
52+
</router-link>
53+
</li>
54+
</ol>`,
55+
...options
56+
}))
57+
}
58+
}
59+
60+
export default new VueBreadcrumbs();

src/plugin.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/shims-vue.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// import VueRouter, { Route } from 'vue-router'
2+
3+
// declare module 'vue/types/vue' {
4+
// interface Vue {
5+
// $router: VueRouter,
6+
// $route: Route
7+
// }
8+
// }
9+
10+
// declare module 'vue/types/vue' {
11+
// interface VueConstructor {
12+
// $breadcrumbs: string
13+
// }
14+
// }

0 commit comments

Comments
 (0)