blazor-page-script
blazor-page-script copied to clipboard
Doesn't work - Cannot get this working in my project
I have followed the steps from the docs to add this to our project and it doesn't work.
Issues:
-
The BlazorPageScript.lib.module.js is not downloaded to the browser. It is located in www folder of RCL, but not downloaded. Please explain how Blazor knows to download this JS file if it is not referenced anywhere. I have no idea how it works in your sample project but it works. If I reference this script explicitly using scrip tags, I get JS error: Unexpected token export FYI: We are using YARP, is that maybe creating problems?
-
Can this be used inside a component (not page, not layout)? We have a use case where we have a component in RCL that needs to re-act to DOM updates.
I figured out problem with item 1: I had to name my module file after the name of my RCL assembly. What about item 2?
For item 2: It appears, that using PageScript inside a component that is inside an RCL and referencing a collocated script on the component works. Any advise against such usage of PageScript component?
Hi @htmlsplash, sorry for the delayed reply. Yes, the project utilizes JavaScript initializers, which utilize the specific naming convention you observed.
You can certainly use PageScript within a non-page component, but I think the use cases for that are limited. The component's main capabilities are:
- Dynamically importing a JS module and running some JS initialization logic after rendering a component
- Reacting to DOM changes caused by enhanced load (streaming update or enhanced navigation)
If your non-page component just needs to import a module or run some JS initialization logic, then PageScript could be helpful. But it doesn't have a way to respond to general-purpose DOM mutation (i.e., mutation not caused by an enhanced page load). For that, MutationObserver might be helpful.
Thanks for the explanation.
As per "inside a component (not page, not layout)", I meant "any" component other than page (ie page component) or layout component.