【问题标题】:Blazor Using Rout Parameters as Method ParametersBlazor 使用 Rout 参数作为方法参数
【发布时间】:2020-05-12 15:06:12
【问题描述】:

在我的 Blazor 组件上,我接受一个参数:

@page "/plant/{PlantName}"
    @code {
        [Parameter]
        public string PlantName { get; set; }
        private TestGarden.Models.Plants pd = new PlantData().GetPlantInformation(PlantName);
    }

在方法“GetPlantInformation”上,它表示“PlantName”-A field initializer cannot reference the non-static field, method, or property 'Plant.PlantName'

我无法将属性 PlantName 设为静态,否则 Blazor 将无法工作。那么如何使用这个属性作为方法参数呢?

【问题讨论】:

    标签: c# asp.net-core blazor asp.net-blazor


    【解决方案1】:

    不确定您要做什么,但您可以使用 => 而不是 =

    //                            added =>
    private TestGarden.Models.Plants pd => new PlantData().GetPlantInformation(PlantName);
    

    您所做的称为Field initialization,使用=> 称为Expression-bodied member(如果名称错误,请有人纠正我)。

    不同的是,当你使用=>时,就像你在这样做

    private TestGarden.Models.Plants pd 
    {
        get 
        {
            return new PlantData().GetPlantInformation(PlantName);
        }
    }
    

    一旦你得到pd,它就会返回new PlantData().GetPlantInformation(PlantName)

    使用=给你错误的原因是你不能用另一个字段的值来初始化一个字段,但是当使用=>时,你并没有初始化它,你只会执行new PlantData().GetPlantInformation(PlantName)时你会得到pd

    编辑:

    正如其他回答指出的那样,您可以使用OnInitialized

    但您需要了解为什么以及何时应该使用OnInitialized=>

    如果 new PlantData().GetPlantInformation(PlantName) 会做一些异步的事情,比如从数据库中获取数据,你应该使用 OnInitialized组件直到数据库返回值。

    如果它从数据库返回一些东西或者做一些可能是异步的,使用OnInitialized,否则使用=>(表达式主体)是可以的。

    还有一件事……

    如果您使用OnInitializednew PlantData().GetPlantInformation(PlantName) 将只运行一次,如果PlantName 因某种原因发生更改,pd 的值将不会更新。

    如果您使用=>,它将始终运行new PlantData().GetPlantInformation(PlantName),但将始终使用PlantName 的当前值(如果它已更改)。

    所以对于如何获得new PlantData().GetPlantInformation(PlantName) 的价值,还有很多事情需要考虑

    【讨论】:

    • @MisterMagoo 不确定我的解释是否可以理解,如果有什么要补充的,请随时编辑。
    【解决方案2】:

    Blazor 组件类似于 C# 对象,但不一样...框架添加了一个语法层,用于通过所谓的“生命周期事件”创建和使用它们。

    对于初始化,您必须重写 OnInitialized() 方法或其异步对应方法 OnInitializedAsync()(如果该初始化需要访问某些异步数据存储):

    protected override async Task OnInitializedAsync()
    {
        pd = await GetPlantInformation(PlantName);
    }
    

    此方法调用只能在创建组件时调用一次。其他生命周期事件请关注Blazor lifecycle

    【讨论】:

      【解决方案3】:

      您不能使用非静态属性 PlantName 初始化 TestGarden.Models.Plants。您应该实例化 TestGarden.Models.Plants 以便使用属性对其进行初始化,这应该在 OnInitialized{Async) 方法中完成。

      这个:

        @code {
          [Parameter]
          public string PlantName { get; set; }
          private TestGarden.Models.Plants pd = new 
          PlantData().GetPlantInformation(PlantName);
       }
      

      应该这样做:

      @code {
          [Parameter]
          public string PlantName { get; set; }
          private TestGarden.Models.Plants pd;
      
        protected override void OnInitialized()
        {
           pd = new PlantData().GetPlantInformation(PlantName);
         }
      
      }
      

      注意:像这样的用法:private Plant plant => new Plant(PlantName);,它的作用可能非常有限,你应该小心使用它,了解你所做的事情,因为它可能会导致不需要的结果。最好的方法是定义一个对象变量,然后在 OnInitialized{Async) 方法中实例化它。这是做它的自然方式,清醒等等......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-21
        • 2014-05-09
        相关资源
        最近更新 更多