Wrapper和Native剃须刀组件之间的区别[关闭]

问题描述 投票:-1回答:2

我对blazor-server-side有一些澄清。 blazor native和wrapper组件有什么区别?谁能帮帮我吗?

asp.net-core blazor blazor-server-side razor-components
2个回答
2
投票

免责声明:我没有使用blazor,但是从第一次演示开始就跟着它一点点,但我从来没有听说过原生和包装组件nor does the documentation include this wording。因此我可能错了,但从我的想法得出的结论是,通过“包装器”实现互操作性。

我将引用this excelent blog post的一些文字

可能是原生的,因为组件仅由.NET bl(r)azor创建,没有javascript:

由于Razor Components将服务器端作为.NET Standard应用程序运行,因此逻辑是使用.NET技术编写的。这是可能的,因为Blazor框架采用了RenderTree,这是一种类似于流行的JavaScript框架(如Angular和React)中使用的虚拟DOM的DOM抽象。让我们看一下框架的UI方面,以了解组件的编写方式。

<p>Current count: @currentCount</p>
<button class="btn btn-primary" onclick="@IncrementCount">
  Click me
</button>

@functions {
  int currentCount = 0;

  [Parameter] protected int CountBy { get; set; } = 1;

  void IncrementCount()
  {
    currentCount += CountBy;
  }
}

可能是包装器,因为我们在我们的组件中使用了JS功能的互操作层。

此外,Razor Components应用程序可以使用JavaScript生态系统的依赖关系,通过互操作性层,应用程序可以与.NET和JavaScript依赖关系进行双向通信。这对于Razor组件不支持必要的浏览器/ DOM API或现有JavaScript库很有用的情况很有用。

GeoLocation.cs(.NET)

public class Geolocation
{
  // ...

  public async Task GetCurrentPosition(
    Action<Position> onSuccess,
    Action<PositionError> onError,
    PositionOptions options = null)
  {
    OnGetPosition = onSuccess;
    OnGetPositionError = onError;
    await JSRuntime.Current.InvokeAsync<bool>(
      "interopGeolocation.getCurrentPosition",
      new DotNetObjectRef(this),
      options);
  }

  // ...
}

interopGeolocation.js(浏览器)

window.interopGeolocation = {
  getCurrentPosition: function (geolocationRef, options) {
    const success = (result) => {
      geolocationRef.invokeMethodAsync(
        'RaiseOnGetPosition',
        interopGeolocation.toSerializeable(result));
    };
    const error = (er) =>
    geolocationRef.invokeMethodAsync(
      'RaiseOnGetPositionError',
      er.code);
    navigator.geolocation.getCurrentPosition(
      success,
      error,
      options);
},

// ...

1
投票
  1. 包装器组件是React中使用的概念或模式。 Blazor中不存在这样的模式。
  2. 服务器端Blazor和客户端Blazor共享相同的组件模型。
  3. 本地Blazor?从来没有听过Blazor人使用这个术语,但是如果你想用这个术语来指代运行Web Assembly的客户端Blazor,请继续。没问题。
  4. 相反,让我介绍一个新术语:Blazor组件。从来没有听过Blazor人使用这个术语,至少不是正式的,但这对我来说听起来很自然和恰当,特别是因为Razor Components已经过时了,应该使用服务器端Blazor和客户端Blazor这两个术语。

希望这可以帮助...

© www.soinside.com 2019 - 2024. All rights reserved.