Skip to content

Commit

Permalink
Adapt document.title to currrent route
Browse files Browse the repository at this point in the history
  • Loading branch information
dmstern committed Dec 3, 2018
1 parent a3dbfba commit c5a3449
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,13 @@ export default class App extends Vue {
});
router.afterEach(this.fireSearchFilterEvent);
router.beforeEach((to, from, next) => {
const getTitle = to.meta.getTitle;
if (getTitle) {
document.title = getTitle(to);
}
next();
});
const anyError: string[] = Object.keys(Errors).map(error => Errors[error]);
Expand Down
22 changes: 21 additions & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
import Vue from 'vue';
import Router from 'vue-router';
import Router, { Route } from 'vue-router';
import Home from './views/Home.vue';
import PackageDetail from './views/PackageDetail.vue';
import HowTo from './views/HowTo.vue';

Vue.use(Router);

const appName = 'npmFrog';
const titleSuffix = ` | ${appName}`;

export default new Router({
routes: [
{
path: '/',
name: 'home',
component: Home,
meta: {
getTitle: () => {
return appName;
},
},
},
{
path: '/package/:scope?/:packageName',
name: 'packageDetail',
component: PackageDetail,
meta: {
getTitle: (route: Route) => {
return `${route.params.scope ? `${route.params.scope}/` : ''}${
route.params.packageName
}${titleSuffix}`;
},
},
},
{
path: '/howto/',
name: 'howto',
component: HowTo,
meta: {
getTitle: (route: Route) => {
return `HowTo${titleSuffix}`;
},
},
},
],
});

0 comments on commit c5a3449

Please sign in to comment.