意外的命名空间导入导入/无命名空间

use*_*969 6 d3.js reactjs plotly plotly-dash

我正在尝试在 React 中导入 D3v4 来生成 Dash 组件。到目前为止,这是我的代码:

import React, {Component} from 'react';
import PropTypes from 'prop-types';
import * as d3 from 'd3';

function createBox(divId) {
    var svgContainer = d3.select(divId).append('svg')
                                     .attr('width', 200)
                                     .attr('height', 200);

 //Draw the Circle
   svgContainer.append('circle')
                        .attr('cx', 30)
                        .attr('cy', 30)
                        .attr('r', 20);
}

/**
 * ExampleComponent is an example component.
 * It takes a property, `label`, and
 * displays it.
 * It renders an input with the property `value`
 * which is editable by the user.
 */
export default class ExampleComponent extends Component {
    constructor() {
        super();
        this.plot = this.plot.bind(this);
    }
    plot(props) {
       createBox(props.id);
    }

    componentDidMount() {
        this.plot(this.props);
    }

    componentWillReceiveProps(newProps) {
        this.plot(newProps);
    }

    render() {
        const {id} = this.props;
        return (
            <div id={id}/>
        );
    }
}

ExampleComponent.propTypes = {
    /**
     * The ID used to identify this compnent in Dash callbacks
     */
    id: PropTypes.string

};
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用$ npm run prepublish它预发布 Dash 时,它会抛出错误:

error Unexpected namespace import  import/no-namespace
Run Code Online (Sandbox Code Playgroud)

它指向import * as d3 from 'd3';

我正在根据Plotly提供的示例构建您自己的组件。

Sag*_*b.g 4

正如esling 文档中提到的:

报告是否使用命名空间导入。

所以你应该把它改成这样:

import d3 from 'd3';
Run Code Online (Sandbox Code Playgroud)

或者,如果您必须使用命名空间(如 eslint 文档中提到的),请禁用该规则

更新
阅读d3 的文档后,他们使用相同的模式,所以我假设没有default导出,d3因此:

import d3 from 'd3';
Run Code Online (Sandbox Code Playgroud)

可能行不通。
然后对各个部分使用命名导入,如下所示:

import {scaleLinear} from "d3-scale";  
Run Code Online (Sandbox Code Playgroud)

或者像我上面提到的那样禁用规则。

  • 我尝试了这个解决方案并且成功了!我只需要将“d3-selection”(我正在使用的库)添加到我的依赖项中。谢谢! (2认同)