嵌套文件夹中的命名空间出现问题



我有一个使用命名空间组件的问题,它在某些地方工作,但不是其他地方:

页面/交易/List.razor

<Test @ref="Test1"></Test>

页面/交易/List.razor.cs:

using Accounting.Web.Components.Test;
namespace Accounting.Web.Pages.Transactions
{
public partial class List
{
private Test Test1 { get; set; } = default!;
}
}

上面对Test的引用工作正常

但是我想在另一个组件中使用Test组件,所以我做了以下操作:

组件/交易/TRansactionRules/TRansactionRules.razor.cs

using Accounting.Web.Components.Test:
namespace Accounting.Web.Components.Transactions.TransactionRules
{
public partial class TransactionRules
{
[Parameter]
public Test Test1 { get; set; } = default!;  // error
}
}

但是在上面的组件中使用它会产生以下错误:

"Test" is a namespace but used as a type

此时我必须用

替换这行
public Accounting.Web.Components.Test.Test Test1 { get; set; } = default!;

而在其他地方,我可以将其称为Accounting.Web.Components.Test

似乎文件夹/文件结构与此有关:

+Pages
+Transactions
-List.razor
-List.razor.cs
+Components
+Test
-Test.razor
-Test.razor.cs  
+Transactions
+TransactionRules
-TransactionRules.razor
-TransactionRules.razor.cs

似乎当我尝试在嵌套在额外子文件夹中的另一个组件中使用Test组件时,引用它失败,否则它将工作。

我希望将其称为Accounting.Web.Components.Test而不是Test。无论在哪里使用,都要进行测试。我做错什么了吗,如果是的话,那又怎样?

我相信你得到的错误是因为你正在"使用"一个命名空间,它也包含了你试图使用的类型的全名。我认为删除。测试从using指令中应该可以解决这个问题。

using Accounting.Web.Components 
namespace Accounting.Web.Components.Transactions.TransactionRules
{
public partial class TransactionRules
{
[Parameter]
public Test Test1 { get; set; } = default!;  // error
}
}

(可选)可以在_Imports中移动下面的代码。Razor文件在blazor解决方案中,并且在组件代码之外。这样,它将使它在您的blazor解决方案中的任何地方都可用,而不仅仅是一个组件文件。

using Accounting.Web.Components

最新更新