2016-07-06 94 views
8

我是全新的Typescript(2周),我在项目上工作以包装d3.js框架。 我遇到了使用“d3.d.ts”,名称空间,导出模块,导入的问题。undefined d3.scale Typescript

我的问题:当我尝试使用d3.scale.linear(),我有错误在我的浏览器控制台:

TypeError: d3.scale is undefined 

代码:

/// <reference path="../typings/d3.d.ts">; 
"use strict"; 
var linear = d3.scale.linear(); 
console.log("linear resolv !") 

我的编译器选项(我在编译过程中没有错误,但也许它很有趣):

{ 
    "files": [ 
     "src/twod3/workspace.ts", 
     "src/twod3/component/basic.ts", 
     "src/twod3/component/data.ts", 
     "src/twod3/component/operation.ts", 
     "src/main.ts" 
    ], 
    "compilerOptions": { 
     "noImplicitAny": true, 
     "target": "es2015", 
     "module":"commonjs", 
     "sourceMap":true 
    } 
} 

我尝试不同的导入策略w没有成功。

的d3.d.ts

declare namespace d3 { 
... 
    export module scale { 
      ... 
      export function linear(): Linear<number, number>; 
      ... 
    } 
} 

我不明白的问题,谢谢你的帮助。

+0

精密:

d3.select(...)
做工精细 –

+4

尝试'd3.scaleLinear'代替'd3.scale.linear'of我猜你是使用D3版本4 – Cyril

+0

这就是工作。谢谢。 –

回答

2

我有一个类似的问题。我不得不匹配D3版本,它支持D3的内置方法。 要么匹配D3.js的版本,请确保你更新了你的凉亭。 或 与您当前版本的内置方法相匹配。

我支持的d3.scale.ordinal版本是'3.5.17',之前我是最新版本。

+0

所以我使用的是[email protected],现在我也需要输入相同的版本,但是当版本3.5.17没有输入时,哪个版本是你跟随? –

26

注意d3.js的版本 在版本4.2.1中d3.scale.linear();给出以下错误。 TypeError:d3.scale.linear()scale is undefined

我使用d3.scaleLinear()修复了版本4.2.1中的此错误;