D3,Typescript -“this”类型的参数不可分配给“BaseType”类型的参数

2024-02-05

我这里有 stackbiltz -https://stackblitz.com/edit/lable-line-break-vaszab?file=src/app/app.component.ts https://stackblitz.com/edit/lable-line-break-vaszab?file=src/app/app.component.ts

我在 Angular 应用程序中有一个 d3 条形图。

使用在创建 z 轴刻度时调用的函数将 x 轴标签分成两行

private insertLinebreak(d) {

    let labels = d3.select(this);
    let words = d;
    console.log("Label:", labels.html());
    labels.text('');

    let index = words.indexOf(' ', words.indexOf(' ') + 1)
    let title = words.substr(0, index)
    let subtitle = words.substr(index + 1)

    let tspantitle = labels.append('tspan').text(title)
    let tspansubtitle = labels.append('tspan').text(subtitle)
    tspantitle
      .attr('x', 0)
      .attr('dy', '15')
      .attr('class', 'x-axis-title');
    tspansubtitle
      .attr('x', 0)
      .attr('dy', '16')
      .attr('class', 'x-axis-subtitle');

  };

该函数使用“this”来选择调用该函数的“g”(但在 d3 中我认为它选择了整个 svg)

这段代码在 stackblitz 中有效,但在我的实际代码中我收到错误

Argument of type 'this' is not assignable to parameter of type 'BaseType'.

我知道这不是很有帮助

我认为这与 Typescript 及其处理“this”的方式有关

有谁知道为什么会发生这种情况以及我该如何解决这个问题。


编译器推断类型this在你的里面insertLinebreak()作为你的班级StackedChartCompoent。看着类型定义 https://github.com/DefinitelyTyped/DefinitelyTyped/blob/444ae0e62284ed7f070c0bf563a2a9ca7fe5e2e5/types/d3-selection/index.d.ts#L123 for d3.select(node),但是,您可以看到它期望节点扩展BaseType这是defined https://github.com/DefinitelyTyped/DefinitelyTyped/blob/444ae0e62284ed7f070c0bf563a2a9ca7fe5e2e5/types/d3-selection/index.d.ts#L21 as

export type BaseType = Element | EnterElement | Document | Window | null;

因为你的课程显然没有扩展这个BaseType你得到了错误。

基本上有两种方法可以解决这个问题:

  1. 如果您需要insertLinebreak()方法仅在一处,即作为回调.each(),您可以将其设为函数表达式,然后将其直接作为参数传递给.each()

    .each(function() {
      let labels = d3.select(this);   // works
      // ...the original method's body
    })
    

    这是可行的,因为编译器现在知道该函数的单个入口点,并且可以推断出该函数的类型this since .each() https://github.com/d3/d3-selection/blob/9ffa69f363b5ee9af2171879d798091eab68def0/src/selection/each.js#L5 uses Function.prototype.call() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call绑定this到节点。

    Keep in mind, though, that you have to use the classic function expression in favor of an ES6 arrow function because that would have this again point to its lexical scope instead of to the node.

  2. 幸运的是,TypeScript 本身内置了一种更惯用的方法。自从2.0版本 http://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#specifying-the-type-of-this-for-functions你可以提供一个假的this范围 http://www.typescriptlang.org/docs/handbook/functions.html#this-parameters作为函数参数列表中的第一项。这个参数告诉编译器什么this指的是所述函数的内部。因此,您的代码可以重写为:

    private insertLinebreak(this: SVGTextElement) {
      let labels = d3.select(this);   // works
      // method body left untouched
    }
    
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

D3,Typescript -“this”类型的参数不可分配给“BaseType”类型的参数 的相关文章

  • 获取类的公共属性而不创建它的实例?

    假设我们有一个 JavaScript 类 var Person function function Person name surname this name name this surname surname Person prototy
  • 所有 'size' 、 'prototype' 的声明必须具有相同的修饰符

    I have downloaded code from repository and run it on local getting these errors have following versions when i run ionic
  • 是否可以在 Ionic 2 中存储 firebase 数据并以离线模式运行应用程序功能?

    我找到了一些关于 Ionic 2 离线模式 firebase 的教程 但我仍然不知道如何在我的应用程序中启用这个功能 那么 firebase真的支持离线模式吗 如果是 我可以存储所有收到的数据吗 and 在离线模式下运行功能 firebas
  • NativeScript:增强 tns-platform-declarations

    在我的 NativeScript 项目中我想包括RecyclerView来自 Android 支持库 我将依赖项包含在app App Resources Android app gradle Uncomment to add recycle
  • 使用许多嵌套的 switchMap 是不好的做法吗?

    我有 HTTP 拦截器 在该拦截器中 在更改请求之前 我需要打开一个加载程序 真正让我担心的是我最终会得到很多switchMaps why 加载器是异步的 我还需要将从拦截器传递的消息转换为加载器服务 翻译消息也是异步的 在拦截器中 我应该
  • Angular 2 中的 Observable 和 ngFor

    下面是一个使用 rxjs Observables 的简单示例 Angular 2 组件 import Component OnInit from angular core import Observable from rxjs Observ
  • Typescript 从传递的函数返回类型推断返回类型

    我可能正在尝试实现不可能的目标 但事情就这样了 我想定义一个函数 function A 它将返回与传递给函数 A 的参数的新函数相同的类型 e g export function test
  • 从 Angular 6 服务中绑定图像

    我有一个端点 它根据某些参数为我提供图像 这不是一个图像网址 而是一个普通图像 因此 当我到达邮递员中的端点时 作为响应 我收到一张图像 JPG 我是否可以在变量中接收该图像并将其绑定到 HTML 标签中 所有问题都有将图像 url 映射到
  • Angular - 使用 routerlink 将对象传递给 @Input 参数

    我有一个带有 Input 参数的角度分量 如下所示 Component selector app transmission history export class TransmissionHistoryComponent implemen
  • Mat-table 多行内的多行

    我想要的内容如下图所示 我使用 Angular Material 7 x 并使用 Mat Table 实现 如下所述 https material angular io components table overview https mat
  • Angular Firebase 电子邮件验证验证后错误

    我正在使用电子邮件和密码设置授权功能 一切正常 但当我创建新用户时 应用程序会发送一封带有验证链接的电子邮件 验证电子邮件地址后 我想登录 以便返回登录表单 emial verified 保持在 假 状态 在我硬重新加载页面后 这是 真 但
  • 赋予 d3 序数轴标签与尺度名称不同

    我有一个序数scale具有不同值的某些标签 我想显示该比例的轴 其中轴标签与比例标签不同 我有这个代码 var width 1000 var height 600 var margins left 100 40 right 25 botto
  • Angular 7 Guard 重定向仅适用于双击

    问题是我已经实现了一个 Guard 旨在处理特定的目录 如果当前用户名的角色等于 2 它应该返回 true 如果没有 那么它不应该重定向 这是我的 app routing module ts 文件 问题出在 userlist 路径中 我们是
  • Angular 2 组件:子对子通信

    我的 Angular 2 应用程序中有三个组件 C0 C1 和 C2 第一个 C0 表示一个 html 模板 具有多个子组件 ui 元素 现在 如果有人点击C1 中的按钮 菜单 我怎样才能通知C2 日历 关于那件事 我尝试了一些例子组件通讯
  • 如何将属性绑定到 style.width (以像素为单位)?

    我正在使用 Angular 2 并且编写了以下代码 div some texts div 我也尝试过 div some texts div export class MyComponent width number 150 但它并不将宽度绑
  • 如何安装旧版本的 Angular?

    我需要安装 Angular 2 但是当我运行时 ng new myApp版本是4 我搜索了很多 但找不到只安装版本2的方法 有谁知道该怎么做 Edit the command ng new MYAPP ng4 false It does n
  • 使用material-ui@next 和 typescript 扩展主题

    创建我的主题时material ui我添加了两个新的调色板选项 为我提供了更好的明暗范围 我已经延长了Theme键入以表明这一点 import Theme from material ui styles import Palette fro
  • 将打字稿中的字符串转换为时间格式

    我必须将服务器数据转换为字符串格式13 47 to 01 47PM但我正在尝试 time date hh MM and task time date shortTime 但它显示日期管道错误和参数错误 运行时错误 InvalidPipeAr
  • 如何检查 Angular 7 中的输入字段是否处于焦点[重复]

    这个问题在这里已经有答案了 我有一个表单 我想知道表单中的任何输入字段是否获得焦点 我读了 NgForm 文档但没有找到任何相关的 focus I found touched但它不能满足需求 您可以使用焦点和模糊事件来跟踪字段获得或失去焦点
  • Angular 7 向原语添加扩展方法

    我想向原语添加一些方法 我有以下文件 字符串扩展 ts interface String isNullOrEmpty this string boolean String prototype isNullOrEmpty function t

随机推荐