Proper way to change a flag in a parent layout from a child page #8603
-
I have the following files: // src/routes/+layout.ts
const load :LayoutLoad = async({depend})=>{
depend("n")
return {n:true}
}
// src/routes/ex/+page.ts
const load :LayoutLoad = async()=>({n:false}) <!-- src/routes/+layout.svelte -->
<script lang=ts>
export let data: LayoutData
</script>
{JSON.stringify(data)}
<a href=/>root</a>
<a href=/ex>ex</a>
<slot/>
<!-- src/routes/+page.svelte -->
Something
<!-- src/routes/ex/+page.svelte -->
Something else
<script>
if(browser)onDestroy(()=>invalidate("n"))
</script> The weird thing is that if I go to I want |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
You could add another page load function for Conversely, your solution that only works with JS shouldn't be an issue. If the user has no JS, they will have a full page reload when navigating and will receive the correct value anyway |
Beta Was this translation helpful? Give feedback.
-
On 20/01/23 07:17, Tee Ming ***@***.***> wrote:
Perhaps something such as:
|// routes/+layout.js export const load = () => { return { n: true } } |
|// routes/+page.js export const load = () => { return { n: true } } |
|// routes/ex/+page.js export const load = () => { return { n: false } } |
This should ensure that
* |/ex| always returns |n: false|
* |/| always returns |n: true|
* all other pages return |n: true| and the layout load function does
not need to re-run.
—
Reply to this email directly, view it on GitHub
<#8603 (reply in thread)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/ABU4KH2UBHJKNFMP2DCZRA3WTIUXDANCNFSM6AAAAAAUAOXM4M>.
You are receiving this because you authored the thread.Message ID:
***@***.***>
The problem is that I have several pages and only /ex should have n=false
|
Beta Was this translation helpful? Give feedback.
You could add another page load function for
/
to changen
to false.Conversely, your solution that only works with JS shouldn't be an issue. If the user has no JS, they will have a full page reload when navigating and will receive the correct value anyway