6

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)

1
  • sounds like you're finding it hard to let go of js? Commented Dec 18, 2019 at 3:49

2 Answers 2

4

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.

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

6 Comments

But that was my question... I dont want to add manually it to host page, I would like a way to ONLY reference my class lib and some how it add the reference to the files
@DouglasSimão This is not yet available, and IMO will not, 'cause you should have a way to choose which contents you would like to add
what does IMO mean?
IMO = In my opinion
@DouglasSimão did you ever get the answer to this? Like you, I feel we should NOT have to add a reference to css in the host app and that it should be automatically included when you add a reference to the component library. If you found out how to do that, I would greatly appreciate knowing the solution.
|
0

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 :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.