Closed CuteLeon closed 4 years ago
Your component PageB.razor
is routable and responds to the base address "/". PageA.razor
is also routable and responds to base address as well. It is reasonable you'll have a conflict. Either PageB.razor
is not routable or change the route so there is no conflict.
Hi @ylr-research :
Thanks for reply.
I tried your solution but it doesn't work...
I removed the **@page**
of Page B, and make sure that there are no pages using the same route address in both project A and project B, but I got the same Exception when I compile.
It seemed that the conflict is about 'wwwroot' folder?
Hi @ylr-research : I found a new solution as below:
Create a .Net Core Class Library project
Microsoft.NET.Sdk.Razor
SDKMicrosoft.AspNetCore.Components.WebAssembly
Nuget package<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.0-rc.1.20451.17" />
</ItemGroup>
</Project>
Create Razor components in Class Library project
You will find Razor Component
when you right click on the project and click Add
Design your component as whatever you like
@inject IServiceProvider provider
@inject ILogger<MyComponent> logger
<h3>My Component in Class Library project !</h3>
<h3>@this.Now.ToString("yyyy-MM-dd")</h3>
@code {
DateTime Now = DateTime.Now;
protected override Task OnInitializedAsync()
{
this.logger.LogInformation("Initialize my component ...");
return base.OnInitializedAsync();
}
}
Render components in the Class Library project
@page "/"
<h1>Hello, world!</h1>
Welcome to your new app.
<ClassLibrary_C.MyComponent></ClassLibrary_C.MyComponent>
But I still want to know what you recommend. Thanks!
Thanks for contacting us. We recommend using Razor Class Library for sharing components between projects.
Thanks for your reply. Hope this will help others.
Describe the bug
Hi Guys: I created two Blazor-WASM projects: BlazorWASM_A and BlazorWASM_B, and I am going to run A project and display B project's Razor component in A.
To Reproduce
Here is my code:
BlazorWASM_A.csproj
PageA.razor of BlazorWASM_A
BlazorWASM_B.csproj
PageB.razor of BlazorWASM_B
Issue
Everytime I try to run Project A, an exception will be thrown:
Exceptions
I readed code of Microsoft.AspNetCore.Components project, and I found that the Components project is using the Microsoft.NET.Sdk SDK while not Microsoft.NET.Sdk.BlazorWebAssembly, and it worked, but what I dont like is that
*.razor
file will not be complied when working on the Microsoft.NET.Sdk SDK, so that I have to design component viaBuildRenderTree(RenderTreeBuilder builder)
in project B. Is there a way to design a component in*.razor
file and use this component in other projects? Waitting for your reply, thanks!Further technical details