Skip to content

Commit

Permalink
feat: migrate to TS, close #70
Browse files Browse the repository at this point in the history
  • Loading branch information
Scrum committed May 14, 2020
1 parent 454bd6f commit bc77070
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 64 deletions.
39 changes: 0 additions & 39 deletions src/components/breadcrumbs.vue

This file was deleted.

6 changes: 0 additions & 6 deletions src/index.js

This file was deleted.

60 changes: 60 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {ComponentOptions, PluginObject, VueConstructor} from 'vue'
import { Route } from 'vue-router';

type CallbackFunction = () => string;

class VueBreadcrumbs implements PluginObject<ComponentOptions<Vue>> {
install(Vue: VueConstructor<Vue>, options = {}) {

Object.defineProperties(Vue.prototype, {
$breadcrumbs: {
get(): Route[] {
return this.$route.matched.map((route: Route) => ({
...route,
path: route.path.length > 0 ? route.path : '/'
}));
}
}
});

Vue.component('Breadcrumbs', Vue.extend({
methods: {
getBreadcrumb(bc: string | CallbackFunction): string {
return typeof bc === 'function' ? bc.call(this, this.$route.params) : bc;
},
getPath(crumb: Route): string {
let {path} = crumb;

for (const [key, value] of Object.entries(this.$route.params)) {
path = path.replace(`:${key}`, value);
}

return path;
}
},
template: `
<ol
class="breadcrumb"
v-if="$breadcrumbs.length"
>
<li
class="breadcrumb-item"
v-if="crumb.meta.breadcrumb"
v-for="(crumb, i) in $breadcrumbs"
:key="i"
>
<router-link
active-class="active"
:to="{ path: getPath(crumb) }"
:tag="i != $breadcrumbs.length - 1 ? 'a' : 'span'"
>
{{ getBreadcrumb(crumb.meta.breadcrumb) }}
</router-link>
</li>
</ol>`,
...options
}))
}
}

export default new VueBreadcrumbs();
19 changes: 0 additions & 19 deletions src/plugin.js

This file was deleted.

14 changes: 14 additions & 0 deletions src/shims-vue.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// import VueRouter, { Route } from 'vue-router'

// declare module 'vue/types/vue' {
// interface Vue {
// $router: VueRouter,
// $route: Route
// }
// }

// declare module 'vue/types/vue' {
// interface VueConstructor {
// $breadcrumbs: string
// }
// }

0 comments on commit bc77070

Please sign in to comment.