[英]Blazor says type or namespace name does not exist in the namespace '__Blazor…'
I'm playing around with Blazor and trying to create a small Grid component. 我在玩Blazor,并尝试创建一个小的Grid组件。 I'd like to pass through configurations via a simple object.
我想通过一个简单的对象进行配置。 The compiler throws the following error:
编译器将引发以下错误:
GridComponent.razor.g.cs(61, 254): [CS0234] The type or namespace name 'GridComponentConfig' does not exist in the namespace '__Blazor.Spa.Shared.Components.Grid' (are you missing an assembly reference?)
The razor and cs files are in the same directory. razor和cs文件位于同一目录中。
<GridComponent data=@modules TItem="Module" Config=@GridComponentConfig></GridComponent>
@functions
{
protected GridComponentConfig GridComponentConfig = new GridComponentConfig
{
Columns = new List<string>
{
"id", "name", "description", "moduleroute"
}
};
}
What am I doing wrong here? 我在这里做错了什么?
GridComponent.razor GridComponent.razor
@using Microsoft.AspNetCore.Components
@typeparam TItem
<table>
<thead>
<GridHeader Data=@Data Config=@Config></GridHeader>
</thead>
<tbody>
</tbody>
<tfoot></tfoot>
</table>
@functions
{
[Parameter]
public IEnumerable<TItem> Data { get; set; }
[Parameter]
public GridComponentConfig Config { get; set; }
}
GridHeader.razor GridHeader.razor
@using Microsoft.AspNetCore.Components
@typeparam TItem
<tr>
@foreach (string column in Config.Columns)
{
<td>@column</td>
}
</tr>
@functions
{
[Parameter]
public IEnumerable<TItem> Data { get; set; }
[Parameter]
public GridComponentConfig Config { get; set; }
}
GridComponentConfig.cs GridComponentConfig.cs
using System.Collections.Generic;
namespace Spa.Shared.Components.Grid
{
public class GridComponentConfig
{
public IEnumerable<string> Columns { get; set; }
}
}
Fix attempt 1: 解决尝试1:
Pages/Components/Grid/GridComponent.razor
Pages/Components/Grid/GridHeader.razor
Common/GridComponentConfig.cs
The @using
s fixed accordingly, example below, by Rider. @using
相应地由Rider修复,例如下面的示例。
GridComponent.razor GridComponent.razor
@using Microsoft.AspNetCore.Components
@using Spa.Common
@typeparam TItem
<table>
<thead>
<GridHeader Data=@Data Config=@Config></GridHeader>
</thead>
<tbody>
</tbody>
<tfoot></tfoot>
</table>
@functions
{
[Parameter]
protected IEnumerable<TItem> Data { get; set; }
[Parameter]
protected GridComponentConfig Config { get; set; }
}
GridHeader.razor GridHeader.razor
@using Microsoft.AspNetCore.Components
@using Spa.Common
@typeparam TItem
<tr>
@foreach (string column in Config.Columns)
{
<td>@column</td>
}
</tr>
@functions
{
[Parameter]
public IEnumerable<TItem> Data { get; set; }
[Parameter]
public GridComponentConfig Config { get; set; }
}
Result : 结果 :
GridComponent.razor.g.cs(57, 231): [CS0234] The type or namespace name 'Common' does not exist in the namespace '__Blazor.Spa' (are you missing an assembly reference?)
Fix attempt 2: 解决尝试2:
Pages/Components/Grid/GridComponent.razor
Pages/Components/Grid/GridHeader.razor
Pages/Components/Grid/GridComponentConfig.cs
The @using
s fixed accordingly, example below, by Rider. @using
相应地由Rider修复,例如下面的示例。
GridComponent.razor GridComponent.razor
@using Microsoft.AspNetCore.Components
@typeparam TItem
<table>
<thead>
<GridHeader Data=@Data Config=@Config></GridHeader>
</thead>
<tbody>
</tbody>
<tfoot></tfoot>
</table>
@functions
{
[Parameter]
protected IEnumerable<TItem> Data { get; set; }
[Parameter]
protected GridComponentConfig Config { get; set; }
}
GridHeader.razor GridHeader.razor
@using Microsoft.AspNetCore.Components
@typeparam TItem
<tr>
@foreach (string column in Config.Columns)
{
<td>@column</td>
}
</tr>
@functions
{
[Parameter]
public IEnumerable<TItem> Data { get; set; }
[Parameter]
public GridComponentConfig Config { get; set; }
}
Result : 结果 :
GridComponent.razor.g.cs(56, 253): [CS0234] The type or namespace name 'GridComponentConfig' does not exist in the namespace '__Blazor.Spa.Pages.Components.Grid' (are you missing an assembly reference?)
Update : 更新 :
You should have a folder named Pages in which you store your components files. 您应该有一个名为Pages的文件夹,用于在其中存储组件文件。 Do not store other types of files in this folder.
不要在此文件夹中存储其他类型的文件。
Define a folder named Grid into which you should move the GridComponentConfig.cs file 定义一个名为Grid的文件夹,您应将GridComponentConfig.cs文件移入其中
In your GridComponent.razor file add @using Spa.Shared.Components.Grid at the top of the page. 在您的GridComponent.razor文件中,在页面顶部添加@using Spa.Shared.Components.Grid。
Hope this helps... 希望这可以帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.