Skip to content

Instantly share code, notes, and snippets.

@IlCallo
Created October 14, 2020 09:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IlCallo/f62ba4939a39f8f3afe1329044e55669 to your computer and use it in GitHub Desktop.
Save IlCallo/f62ba4939a39f8f3afe1329044e55669 to your computer and use it in GitHub Desktop.
import { inject, ref, provide } from '@vue/composition-api';
export function useAppBarProviders() {
const title = ref('');
function updateTitle(newTitle: string) {
title.value = newTitle;
}
provide('updateAppBarTitle', updateTitle);
return { title };
}
export function useAppBarInjections(title?: string) {
const updateAppBarTitle = inject<(title: string) => void>(
'updateAppBarTitle'
);
if (!updateAppBarTitle) {
throw new Error(
"We could not find injections provided by 'useAppBarProviders', make sure to call it on an ancestor of this component"
);
}
if (title) {
updateAppBarTitle(title);
}
return { updateAppBarTitle };
}
<script lang="ts">
import { defineComponent } from '@vue/composition-api';
import { useAppBarInjections } from 'composables/app-bar';
export default defineComponent({
name: 'PageHome',
setup() {
useAppBarInjections('Home page title');
}
});
</script>
<script lang="ts">
import { defineComponent } from '@vue/composition-api';
import { useAppBarProviders } from 'composables/app-bar';
export default defineComponent({
name: 'MainLayout',
setup() {
const leftDrawerOpen = ref(false);
return {
leftDrawerOpen,
...useAppBarProviders(),
};
},
});
</script>
<template>
<q-layout view="lHh Lpr lFf">
<q-header elevated>
<q-toolbar>
<q-btn
flat
dense
round
icon="mdi-menu"
aria-label="Menu"
@click="leftDrawerOpen = !leftDrawerOpen"
/>
<q-toolbar-title class="text-weight-bold q-ml-lg">{{
title
}}</q-toolbar-title>
</q-toolbar>
</q-header>
<!-- other stuff -->
</q-layout>
</template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment