Created
October 14, 2020 09:37
-
-
Save IlCallo/f62ba4939a39f8f3afe1329044e55669 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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