During work on application we faced a need to use mixin for pages only. Right now we have two options:
const isPage = this.$options.name && this.$options.name.toLowerCase().endsWith('page')
, but this is very fragileAs an example it would be nice to have this.$options.isPage
boolean
Ok, found a property $metaInfo
. Is it only for pages?
Update: nope
Maybe, there is a way to add such property to page component during build?
I would suggest to use directly a PageComponent
that you create with the mixin inside and use it into your pages directly:
import PageComponent from '~/components/Page.vue'
export default PageComponent.extend({
// asyncData
// ...
})
This will avoid to have any kind of global Mixin that has an impact on performances.
What is your mixin exactly and what are you trying to achieve with it that you can't do with another way?
This way we will be able to find the best solution :-)
@Atinux so, do I need to extend each page I have in my project?
@Atinux what I am trying to achieve is to add schema.org markup to each page using head ()
function in mixin
I think an option to inject a mixin only into page components would be great, because it allows to keep .vue pages without script blocks.
Is there any update on this?
For those who are come across on this issue and having the problem, I found a workaround because I wanted to create a global breadcrumb mixin. This solution is working(using beforeRouteEnter):
const installPlugin: PluginFunction<any> = (Vue) => {
Vue.mixin({
beforeRouteEnter(to, from, next) {
next((vm) => {
const options = vm.$options;
const breadcrumb = options.breadcrumb;
if (breadcrumb != null && typeof breadcrumb === 'function' && vm.$store != null) {
const list = breadcrumb.call(vm);
BreadCrumbModule.helpers.updateState({ list });
} else if (vm.$store != null && BreadCrumbModule.state.list.length > 0) {
BreadCrumbModule.helpers.updateState({ list: [] });
}
});
},
});
};
Most helpful comment
I think an option to inject a mixin only into page components would be great, because it allows to keep .vue pages without script blocks.