d3和webpack:__ WWEPACK_IMPORTED_MODULE_0_d3 __ .scaleLinear不是函数

时间:2018-04-28 16:38:21

标签: d3.js webpack

我正在尝试使用d3在我的Vuejs组件中渲染一些图形。我按照推荐的import * as d3 from 'd3'导入d3,并尝试在其上调用d3函数。

webpack.base.conf.js

'use strict'
const path = require('path')
const utils = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./vue-loader.conf')

function resolve (dir) {
  return path.join(__dirname, '..', dir)
}

const createLintingRule = () => ({
  test: /\.(js|vue)$/,
  loader: 'eslint-loader',
  enforce: 'pre',
  include: [resolve('src'), resolve('test')],
  options: {
    formatter: require('eslint-friendly-formatter'),
    emitWarning: !config.dev.showEslintErrorsInOverlay
  }
})

module.exports = {
  context: path.resolve(__dirname, '../'),
  entry: {
    app: './src/main.js'
  },
  output: {
    path: config.build.assetsRoot,
    filename: '[name].js',
    publicPath: process.env.NODE_ENV === 'production'
      ? config.build.assetsPublicPath
      : config.dev.assetsPublicPath
  },
  resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
      '@': resolve('src'),
      'src': resolve('src'),
      'components': resolve('src/components'),
      'assets': resolve('src/assets'),
      'static': resolve('static'),
      'styles': resolve('src/styles')
    }
  },
  module: {
    rules: [
      ...(config.dev.useEslint ? [createLintingRule()] : []),
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: vueLoaderConfig
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('img/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('media/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
        }
      },
    ]
  },
  node: {
    // prevent webpack from injecting useless setImmediate polyfill because Vue
    // source contains it (although only uses it if it's native).
    setImmediate: false,
    // prevent webpack from injecting mocks to Node native modules
    // that does not make sense for the client
    dgram: 'empty',
    fs: 'empty',
    net: 'empty',
    tls: 'empty',
    child_process: 'empty'
  }
}

.babelrc

{
  "presets": [
    ["env", {
      "modules": false,
      "targets": {
        "browsers": ["> 1%", "last 2 versions", "not ie <= 8"]
      }
    }],
    "stage-2"
  ],
  "plugins": [
    "dynamic-import-node",
    "syntax-dynamic-import",
    "transform-runtime"
  ],
  "env": {
    "test": {
      "presets": [
        [ "env", { "targets": { "node": 8}}],
      ],
      "plugins": ["transform-es2015-modules-commonjs", "dynamic-import-node"]
    }
  }
}

来自控制台的错误:Vue warn]: Error in mounted hook: "TypeError: __WEBPACK_IMPORTED_MODULE_0_d3__.scaleLinear is not a function"

使用d3的组件:

import * as d3 from 'd3'
const graphW = '200'
const graphH = '30'
const xDomain = [1, 10]
const xRange = [0, 200]
const yRange = [30, 0]

export default {
    ...
    methods: {
        render: function () {
            var self = this
            var graph = d3.select('.' + self.className).append('svg:svg').attr('width', graphW).attr('height', graphH)
            var data = self.data

            var x = d3.scaleLinear().domain(xDomain).range(xRange)
            var y = d3.scaleLinear().domain([d3.min(data), d3.max(data)]).range(yRange)
    ...

2 个答案:

答案 0 :(得分:0)

在使用D3 v5的一些D3示例时,我遇到了类似的错误:

  

[Vue警告]:挂钩错误:&#34; TypeError:    WEBPACK_IMPORTED_MODULE_0_d3 .schemeCategory10不是函数&#34;

我通过从scaleLinear删除括号来解决它,更改:

var x = d3.scaleLinear().domain(xDomain).range(xRange)
var y = d3.scaleLinear().domain([d3.min(data), d3.max(data)]).range(yRange)

为:

var x = d3.scaleLinear.domain(xDomain).range(xRange)
var y = d3.scaleLinear.domain([d3.min(data), d3.max(data)]).range(yRange)

答案 1 :(得分:0)

试试

.scale.linear()

代替 .scaleLinear()

我认为 npm 版本不同。