Skip to content

Instantly share code, notes, and snippets.

@japalekhin
Last active May 19, 2019 16:34
Show Gist options
  • Save japalekhin/c08606e805d4a3577dc4c45988eca6de to your computer and use it in GitHub Desktop.
Save japalekhin/c08606e805d4a3577dc4c45988eca6de to your computer and use it in GitHub Desktop.

mixin

./src/mixins/title.js

function getTitle(vm) {
  const { title } = vm.$options;
  if (title) {
    return typeof title === 'function'
      ? title.call(vm)
      : title;
  }
}

export default {
  mounted() {
    const title = getTitle(this);
    if (title) {
      document.title = title;
    }
  }
};

main

./src/main.js

import mixinTitle from './mixins/title';

Vue.mixin(mixinTitle);

route component (view)

./src/views/example.vue

<script>
export default {
  title: 'Example page title'
};
</script>

or: ./src/views/example2.vue

<script>
export default {
  title() {
    const someValue = 'Another Example';
    return `Example page title: ${someValue}`;
  }
};
</script>

Based on Evan You's title mixin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment