-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Add compiler error if there's a <script> element inside a component #16218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
warning rather than error? |
No, definitely an error! You can't have local scripts for the reasons described above - that's the whole point 😃 |
Lets say I want to write a "Canvas" component that has a bunch of Csharp calls to javascript functions. Would there be a way to go about doing that? |
@tjbortz1s Yes, use the existing .NET->JS interop APIs. This doesn't involve putting a |
@SteveSandersonMS it could be an issue to integrate web components if we cannot use local script, what you think ? |
@aguacongas Yes, we’ve already implemented a mechanism for creating redistributable packages of components that can load custom JS automatically. There’ll be full info about that in the 0.2.0 release blog post and docs, which we’re now targeting for Monday 😄 |
great |
implemented a mechanism for creating redistributable packages of components Does that mean you can make something like a select/dropdown component as an example, then pack it something like a NuGet package to share/reuse? |
Yes |
How would I open a modal bootstrap dialog from blazor. Do I need to create for each component/page a function to open the modal dialog and create one js file I include in index.html. I believe it's a bad idea not to allow a script on each page/component. This would make the code more readable and cleaner. Each page/component would be seperated from other and so more readable. |
@Knudel well you can define helpers like Blazor.registerFunction('ShowBootstrapModal', function (element) {
var modal = new Modal(element, { backdrop: false });
modal.show();
return true;
});
Blazor.registerFunction('HideBootstrapModal', function (element) {
var modal = new Modal(element);
modal.hide();
return true;
}); public static class Bootstrap
{
public static void ModalShow(ElementRef element) => RegisteredFunction.Invoke<bool>("ShowBootstrapModal", element);
public static void ModalHide(ElementRef element) => RegisteredFunction.Invoke<bool>("HideBootstrapModal", element);
} and call them like this <div class="modal fade" ref="SomeModal" tabindex="-1" role="dialog">
</div>
@functions
{
private ElementRef SomeModal;
void ShowIt()
{
Bootstrap.ModalShow(SomeModal);
}
} |
Thank you Suchiman, that would indeed solve my problem. I didn't know of ref. |
i'm trying this, but its not working with me , am i missing something ??? my \wwwroot\index.html <script type="blazor-boot">
// Register a very simple JavaScript function that just prints
// the input parameter to the browser's console
Blazor.registerFunction('say', (data) => {
console.dir(data);
// Your function currently has to return something. For demo
// purposes, we just return `true`.
return true;
});
</script> and my \Pages\FetchData.cshtml
but i'm getting this error
|
@lakani the |
it works 💯 |
Thank you everyone for all the information you provided. I would like to know if there's a way we can only load the JS libraries based on the component? For example, let's say a specific JS file is only valid for one component. How can I possibly load the JS file when that specific component is called? We have a huge project with about 100 JS files, most of which specific to a single view. We would like to reuse them until we completely migrate to Blazor eventually. If I load all the JS files in |
@sethi-ishmeet I'd recommend writing a small bit of JS that loads other JS files, and then calling it from your components via the JS interop APIs. On the other hand, it's worth trying to estimate how big your bundles would be if you just put them all into one file, e.g., using WebPack. You might find that because of compression, the combined size of all 100 JS files is much less than 100 times their average size. If the whole set with compression is < 100KB, for instance, you're probably much better just loading them all up front. |
Thank you @SteveSandersonMS I think that'd solve our purpose. |
What is the best practice for inserting MathJax into a component? If you're not familiar, MathJax is a meta-language that looks like
which is really syntactic sugar for something like
This script block is then evaluated to (extremely verbose) MathML when a static page is loaded. I understand it's always possible to generate the MathML in a static page, and then paste that output into my component (which is what I've been doing). But to keep the maintenance straightforward, I would prefer to be able to insert MathJax directly into my component using the |
@PaulNWms This issue is closed. I recommend posting a new issue with your question on https://github.com/aspnet/aspnetcore/issues so we can track your scenario. |
It will be better to have new issue link on this template https://github.com/aspnet/AspNetCore/issues/new/choose |
@iAmBipinPaul Good idea! |
@iAmBipinPaul Actually, it looks like the issue template does point folks to the https://github.com/aspnet/aspnetcore repo already. |
@danroth27 I see. May be we can have something like this on template and it will show URL there directly.
|
I would like the flexibility and the ability to easily choose. As simple as that. |
For anyone who understands the caveats of the original post, and still wants to do this (e.g. because you value encapsulation, or because of convenience, etc.), beware, adding a But fear not, for there is an easy workaround! 😉In your
Then to use it, it's just:
Anyways, you're welcome. 👍 |
This addresses a usability issue. We often hear of people trying to put
<script>
elements inside components and being surprised about the effects.It's almost always a mistake to put a
<script>
element inside a component, because fundamentally, components are about adding and removing things dynamically inside the DOM.<script>
elements don't work that way, in that once they are added, their effects can never be removed. Similarly, trying to use C# expressions inside a<script>
content will not work as expected (we can't replace the effects of already-registered JS once the expression changes value). The only scenario I can think of where a<script>
element does something vaguely useful inside a component would be for some kind of debug logging, e.g.,<script>console.log('rendering now')</script>
, but that's not what anyone's been doing AFAIK. What developers really should do is put<script>
elements into theirindex.html
, where they will behave as expected.Proposed solution
We'll make it a compiler error to have
<script>
inside a Blazor component. The error message will say something like:Script tags should not be placed inside components because they cannot be updated dynamically. To fix this, move the script tag to the 'index.html' file or another static location. For more information see http://some/link
... and the help docs that we linked to will also say something like:
If you need to override this, add an attribute named "suppress-error" with value "BL9992" to the <script> tag, but only if you know what you are doing, as in most cases it would be a mistake to do this.
The text was updated successfully, but these errors were encountered: