Skip to content
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

Build error when parameter and child component have the same name #5634

Closed
fayezmm opened this issue Nov 7, 2018 · 3 comments
Closed

Build error when parameter and child component have the same name #5634

fayezmm opened this issue Nov 7, 2018 · 3 comments
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug.

Comments

@fayezmm
Copy link

fayezmm commented Nov 7, 2018

Add support for @tagHelperPrefix

Problem

Current implementation of Blazor v0.6.0 does not have support for Razor @tagHelperPrefix

Functional impact

Trying to Include a child component inside a RenderFragment of a parent component where both the child and the RenderFragment have the same name will result in compile time error.

Minimal repro steps

Let us suppose we have a component in Panel.cshtml as follows:

<div class="panel panel-default">
  <div class="panel-heading">@PanelHeader</div>
  <div class="panel-body">@PanelBody</div>
</div>

@functions{
  [Parameter] private RenderFragment PanelHeader { get; set; }
  [Parameter] private RenderFragment PanelBody { get; set; }
}

and another component in PanelHeader.cshtml like this:

<div>@Title</div>
<div>@SubTitle</div>

@functions{
  [Parameter] private string Title { get; set; }
  [Parameter] private string SubTitle { get; set; }
}

then using these two components in a page like below code will result in a compile time error:

<Panel>
  <PanelHeader>
       <PanelHeader Title="some title" SubTitle="sub title"></PanelHeader>
  </PanelHeader>
  <PanelBody>Some content ...</PanelBody>
<Panel>

Proposal

I would recommend to add support for @tagHelperPrefix so to simplify distinguishing components from RenderFragments.

Above code could then can be re-written as follows:

Panel.cshtml would be like this:

@tagHelperPrefix "pre1:"
<div class="panel panel-default">
  <div  class="panel-heading">@PanelHeader</div>
  <div  class="panel-body">@PanelBody</div>
</div>

@functions{
  [Parameter] private RenderFragment PanelHeader { get; set; }
  [Parameter] private RenderFragment PanelBody { get; set; }
}

And PanelHeader.cshtml would look like this:

@tagHelperPrefix "pre2:"
<div>@Title</div>
<div>@SubTitle</div>

@functions{
  [Parameter] private string Title { get; set; }
  [Parameter] private string SubTitle { get; set; }
}

And finally both the child and the parent components can be used like this:

<pre1:Panel>
  <PanelHeader>
       <pre2:PanelHeader Title="some title" SubTitle="sub title"></pre2:PanelHeader>
  </PanelHeader>
  <PanelBody>Some content ...</PanelBody>
<pre1:Panel>
@danroth27
Copy link
Member

Thanks for this suggestion!

We don't plan to add support for @tagHelperPrefix because the intent is for components and parameters to follow the same name resolution rules as .NET types and properties. For example if you need to namespace qualify a component name you would simply specify the namespace in addition to type name: <Namespace.MyComponent />. This isn't implemented yet, but is tracked by https://github.com/aspnet/Blazor/issues/1315.

That said, your scenario should work today as is, and the fact that it doesn't I think is a bug. It's the equivalent of having a property and type with the same name, which should be fine.

@fayezmm
Copy link
Author

fayezmm commented Nov 7, 2018

Oh that will be great but at the same time would be very lengthy to type in all the namespaces. Perhaps, when implemented you may add a feature to provide something like using statement in C#

@danroth27
Copy link
Member

@fayezmm Yup, the good news is Razor already has a @using directive for that.

@danroth27 danroth27 changed the title Add support for @tagHelperPrefix Build error when parameter and child component have the same name Nov 7, 2018
@aspnet-hello aspnet-hello transferred this issue from dotnet/blazor Dec 17, 2018
@aspnet-hello aspnet-hello added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates bug This issue describes a behavior which is not expected - a bug. area-blazor Includes: Blazor, Razor Components labels Dec 17, 2018
@mkArtakMSFT mkArtakMSFT removed area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates labels May 9, 2019
@dotnet dotnet locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug.
Projects
None yet
Development

No branches or pull requests

4 participants