[英]Problem with namespace from a nested folder
我在使用命名空間組件時遇到問題,該組件在某些地方有效但在其他地方無效:
頁面/事務/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.Test,無論它在哪里使用。 我做錯了什么,如果是這樣怎么辦?
我相信您得到的錯誤是因為您正在“使用”一個名稱空間,該名稱空間還包含您嘗試使用的類型的全名。 我認為從 using 指令中刪除.Test應該可以解決這個問題。
using Accounting.Web.Components
namespace Accounting.Web.Components.Transactions.TransactionRules
{
public partial class TransactionRules
{
[Parameter]
public Test Test1 { get; set; } = default!; // error
}
}
(可選)您可以將以下代碼移至 blazor 解決方案中的 _Imports.razor 文件內部,以及組件代碼外部。 這樣一來,它將使其在您的 blazor 解決方案中隨處可用,而不僅僅是一個組件文件。
using Accounting.Web.Components
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.