gpt4 book ai didi

d3.js - d3 和 webpack : __WEBPACK_IMPORTED_MODULE_0_d3__. scaleLinear 不是函数

转载 作者:行者123 更新时间:2023-12-03 09:42:50 25 4
gpt4 key购买 nike

我正在尝试使用 d3 在我的 Vuejs 组件中呈现一些图形。我正在按照推荐的方式导入 d3 import * as d3 from '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)
...

最佳答案

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

[Vue warn]: Error in mounted hook: "TypeError: WEBPACK_IMPORTED_MODULE_0_d3.schemeCategory10 is not a function"



我通过从 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)

关于d3.js - d3 和 webpack : __WEBPACK_IMPORTED_MODULE_0_d3__. scaleLinear 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50078781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com