简体   繁体   English

为什么在尝试将转译的 js 文件导入我的应用程序时会收到 $ isotafunction 和 window.renderDashboardisnotafunction 的错误?

[英]Why am I getting errors of $ isnotafunction and window.renderDashboardisnotafunction when trying to import a transpiled js file into my application?

I have a React Component that uses @nivo/line.我有一个使用 @nivo/line 的 React 组件。 I have had no issues working on the component itself, but I am unable to render it in my rails application.我在处理组件本身时没有遇到任何问题,但我无法在我的 Rails 应用程序中呈现它。 I am inserting it into an html.erb.file like this:我将它插入到 html.erb.file 中,如下所示:

<script src="<%=root_url%>DogDashboard.js">    
</script>

index.js of the application I am trying to insert into:我试图插入的应用程序的 index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import { Dashboard } from '@reponame/projectrepo';
window.renderDashboard = data =>
    ReactDOM.render(<DogDashboard linedata={data}/>, document.getElementById('name-of-dogdiv'));

App.js of React Component React 组件的 App.js

import React, { Component } from 'react';
import './App.css';


import { Chart } from './components'

class App extends Component {
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <Chart data = {this.props.linedata} />
        </header>
      </div>
    );
  }
}

export default App;

index.js of React Component React 组件的 index.js

import "core-js/stable";
import "regenerator-runtime/runtime";
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import linedata from './linedata.json'

ReactDOM.render(<App linedata={linedata}/>, document.getElementById('name-of-dogdiv'));

serviceWorker.unregister();

The errors I am getting are:我得到的错误是:

Uncaught TypeError: $ is not a function
    at Module../node_modules/core-js/modules/es.array.index-of.js (DogDashboard.js:18067)
    at __webpack_require__ (DogDashboard.js:20)
    at Module.<anonymous> (DogDashboard.js:16084)
    at Module../node_modules/core-js/internals/regexp-exec.js (DogDashboard.js:16186)
    at __webpack_require__ (DogDashboard.js:20)
    at Module../node_modules/core-js/modules/es.regexp.exec.js (DogDashboard.js:20138)
    at __webpack_require__ (DogDashboard.js:20)
    at Module.<anonymous> (DogDashboard.js:14317)
    at Module../node_modules/core-js/internals/indexed-object.js (DogDashboard.js:14335)
    at __webpack_require__ (DogDashboard.js:20)

and,和,

Uncaught TypeError: window.renderDashboard is not a function
    at Object.success ((index):520)
    at c (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
    at l (jquery.min.js:2)
    at XMLHttpRequest.<anonymous> (jquery.min.js:2)

webpack config:网络包配置:

var path = require('path');
module.exports = {
    mode: 'development',
    entry:  [path.join(__dirname, 'index.js')],
    output: {
        path: path.join(__dirname, 'out'),
        filename: 'DogDashboard.js'
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules\/(?!(@rtls)\/).*/,
                use: [
                    {
                        loader: 'babel-loader',
                        options: {
                            "presets": [
                                [
                                    "@babel/preset-env", {
                                        "targets":  {
                                            "chrome": "58",
                                            "ie": "11"
                                        },
                                        useBuiltIns: 'usage',
                                        corejs: '3.0.0',
                                    },
                                ],
                            "@babel/preset-react"
                            ]
                        },
                    },
                ]

            },
            { 
                test:/\.css/,
                use: ['style-loader', 'css-loader'],
            },
        ],
    },
    plugins: [
        new webpack.ProvidePlugin({
            Promise: 'es6-promise',
            fetch: 'exports-loader?global.fetch!isomorphic-fetch',
        }),
    ],
    resolve: {
        extensions: ['*', '.js'],
    },
    stats: {
        colors: true
    },
    devtool: 'source-map'
}

Things I have tried: * reverse engineering it and specifically seeing where this breaks, but any upgrade I do to the component, breaks the rendering of it.我尝试过的事情: * 对它进行逆向工程,特别是查看它在哪里中断,但是我对组件所做的任何升级都会破坏它的渲染。 * verified dependencies in package.json * reviewed git history for accidental code changes * limiting the scope of window.renderDashboard * changing the vars to let (even though they would have the same result in my context, but as a sanity check) * adding quotes ( to "”) * removing "window" and making a new global object * removing the lambda and making it a regular function using keyword function in * 验证 package.json 中的依赖项 * 审查意外代码更改的 git 历史 * 限制 window.renderDashboard 的范围 * 将 vars 更改为 let(即使它们在我的上下文中具有相同的结果,但作为完整性检查) * 添加引号 ( to "") * 删除“window”并创建一个新的全局对象 * 删除 lambda 并使用关键字 function in 使其成为常规函数

window.renderDashboard = data => ReactDOM.render(<DogDashboard linedata={data}/>, document.getElementById('name-of-dogdiv'));

Please help.请帮忙。 Thanks.谢谢。

I ended up starting fresh from a new create-react-app and putting my chart over it.我最终从一个新的 create-react-app 重新开始,并将我的图表放在上面。 I couldn't figure out what was causing this.我无法弄清楚是什么导致了这种情况。 I am guessing conflicts with dependencies, particularly with core-js.我猜测与依赖项冲突,尤其是与 core-js 冲突。 Upgrading dependencies was not helpful.升级依赖项没有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在尝试导入我的函数时,为什么会出现“TypeError:Assignment to constant variable”? - Why am I getting “TypeError: Assignment to constant variable” when trying to import my function? 当我尝试使用React从另一个文件导入类时,为什么会出现空白屏幕 - Why am I getting a blank white screen when trying to import a class from another file using React 为什么在尝试导入 CSS 文件时出现 SyntaxError:导入声明可能只出现在模块的顶层? - Why am I getting SyntaxError: import declarations may only appear at top level of a module, when trying to import a CSS file? 为什么在尝试运行我的代码时会收到 TypeError? - Why am I getting a TypeError when trying to run my code? 为什么我的JavaScript出错? - Why am I getting errors with my JavaScript? 如何将 mongoose 中的鉴别器连接到我的父文档? 尝试导入数据时出错 - How do I connect my discriminators in mongoose to my parent document? Getting errors when trying to import data 为什么我的.js文件出现错误? - Why am I getting an error in my .js file? 当我尝试调用我的方法时,为什么我会变得不确定? - Why am I getting undefined when I am trying to call my method? 我正在尝试将国家/地区列表放在 a.js 文件中并将其导出以供重复使用。 得到错误 - I am trying to put a list of countries in a .js file and export it for reuse. Getting errors 我没有收到任何错误,但是当我尝试单击“表格”时,我的表格类型鼠标没有出现 - I am not Getting Any Errors but When i am trying to click on Form my form Type Mouse does not appear
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM