0

Regarding this doc from Microsoft - https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/static-server-rendering?view=aspnetcore-9.0

Does anyone know how to either pass data to, or access property values from the related razor file in these enhanced navigation js functions:

export function onLoad() {
  console.log('Loaded');
}

export function onUpdate() {
  console.log('Updated');
}

export function onDispose() {
  console.log('Disposed');
}

e.g. In the same docs there's this example razor file:

@page "/page-with-script"
@using BlazorPageScript

<PageTitle>Enhanced Load Script Example</PageTitle>

<PageScript Src="./Components/Pages/PageWithScript.razor.js" />

Welcome to my page.

Suppose it also had this:

@code {
    public string SomeProp { get; set; } = "Hello, World!;
}

How could I pass "SomeProp" to the onUpdate() function? Although this function is triggered by an "enhancedload" so probably can't pass to it, but can I access the razor property from the function somehow?

1
  • Create an IJSObjectReference for your .js file and then use InvokeAsync or InvokeVoidAsync to pass in your values to your onUpdate function. Commented Mar 3 at 11:36

1 Answer 1

1

You could try this workaround to hide the value in dom

<PageScript Src="./Components/Pages/PageWithScript.razor.js" />
<p id="test" hidden="hidden">@SomeProp</p>
@code {
    public string SomeProp { get; set; } = "Hello, World!";
}

Then get the value using document.

export function onLoad() {
    console.log('Loaded');
    var prop = document.getElementById("test").innerHTML;
    console.log(prop);
}

Test result
enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.