为 Power BI 自定义视觉开发安装 d3 类型时遇到问题

Trouble Installing d3 Typings for Power BI Custom Visual Development

我在安装 d3 类型时遇到问题。我按照 Microsoft 在 https://github.com/Microsoft/PowerBI-visuals/blob/master/Tutorial/Typings.md as well as what Sachin Patney does in his video at https://www.youtube.com/watch?v=_2-yMGtEv2w.

的说明进行了操作

运行 "npm install typings -g" 似乎工作正常。

运行 "typings install --save --global dt~d3" 或 "typings i dt~d3 -G" 都会产生此错误:"typings ERR! message Attempted to compile "d3" 作为全局模块,但它看起来像外部模块。您需要删除全局选项以继续。"

如果我删除全局选项,它会使用路径中的 "modules" 文件夹而不是 "globals" 文件夹(即 MyVisual|typings|modules| d3 而不是 MyVisual|typings|globals|d3)。 Intellisense 也不适用于 d3。

知道为什么我不能全局安装 d3 typings 吗?

是的,我也遇到了同样的问题,这样做你会没事的: 手动删除文件夹内的所有内容"Typings" 打开 Windows powershell 并输入以下命令:

npm install @types/d3

打开tsconfig.json修改如下:

{
  "compilerOptions": {
    "allowJs": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "ES5",
    "sourceMap": true,
    "out": "./.tmp/build/visual.js"
  },
  "files": [
    ".api/v1.3.0/PowerBI-visuals.d.ts",
    "node_modules/@types/d3/index.d.ts",
    "src/visual.ts"
  ]
}

通过这种方式我得以继续,如果它也适用于您,请告诉我。

虽然@FabioEnne 的回答确实解决了我关于全局安装和智能感知的问题,但我仍然 运行 进入与该问题的最初原因相关的其他问题。但我想我找到了解决方法...

根据 Jon Gallant 的说法:

The Power BI team just released v1.2 of the Custom Visuals SDK. With this version you now need to reference d3 v3.5.5 yourself. d3 v4 does not work yet. I’m working with the team to get a v4 compat and sample together, but for now you can only use v3.5.5.

(@FabioEnne 的解决方案将 v4.4.0 添加到我的系统中。)

Jon 在他的网站上提供了这个问题的解决方案:http://blog.jongallant.com/2016/11/pbiviz-12-d3-35-reference/。 (他附上了一段视频。)

Jon 解决方案的要点是:

安装类型:

npm i -g typings

添加 d3 v3.5.5:

npm i d3@3.5.5 --save

添加 d3 类型:

typings install d3=github:DefinitelyTyped/DefinitelyTyped/d3/d3.d.ts#6e2f2280ef16ef277049d0ce8583af167d586c59 --global --save

将文件添加到 tsconfig.json:

{
  "compilerOptions": {
    "allowJs": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "ES5",
    "sourceMap": true,
    "out": "./.tmp/build/visual.js"
  },
  "files": [
    ".api/v1.3.0/PowerBI-visuals.d.ts",
    "typings/index.d.ts",
    "node_modules/d3/d3.min.js",
    "src/visual.ts"
  ]
}