Reputation: 289
Is there a way to when creating a Razor class library and in there you have js, css it will automatically add the reference links in _Host.cshtml from Blazor app?
making it short I do not want to add a reference for my Css/js from my razor class lib to another blazor app Host (layout)
Upvotes: 6
Views: 2881
Reputation: 1250
IF I am understanding the question correctly and fwiw (for what its worth) I have to agree that having to manually copy an RCL dependency, like a JS module, to the host project in order to, say, instantiate an object ostensibly provided by the RCL breaks the idea of a self-contained UI library. It's not.
For example, if the RCL provides say a "Pager" JS object and the Host JS can't do
let pager = new Pager()
The RCL then can't be packaged for NuGet installation without scripting the install (and I'm not even sure that it can be done there) and worse, arbitrarily stuffing a JS file into the host project's unknown file structure.
Supposedly static resources can be embedded into an RCL but in the case of JS, there doesn't seem to be any way to reference said resource directly from the Host's own scripts.
IMO that greatly reduces the usefulness of the RCL over say View Components or Partial Views, so....
if anyone knows how to accomplish this I'd be grateful for a post :-)
Upvotes: 0
Reputation: 17404
Put your dependencies in library's wwwroot folder.
In your _Host.cshtml add css and scripts links to your libraries contents :
<!DOCTYPE html>
<html>
<head>
...
<link href="_content/Aguacongas.AwsComponents/styles.css" rel="stylesheet" />
</head>
<body>
<app>
</app>
<script src="_content/Aguacongas.AwsServices/main.bundle.js"></script>
<script src="_framework/blazor.server.js"></script>
</body>
</html>
In this sample I add css from Aguacongas.AwsComponents library and scripts from Aguacongas.AwsServices library.
For Blazor WASM do the same in your index.html or any other html host page.
Upvotes: 4