首页 > 解决方案 > 如何在 ReactJS 应用程序中插入第 3 方库

问题描述

它在纯 JS 中完美运行,但现在我试图在我的 ReactJS 应用程序中导入 KaiOS SDK 的库,但我不能,编译总是失败并显示此消息:

  Line 230:9:  'getKaiAd' is not defined  no-undef

这该怎么做 !?(js库文件

这是我的简化代码,我们可以看到第 12 行的函数调用失败..

import React, { Component } from 'react'

import './App.css'
import Result from './Result'
import BoutonListe from './BoutonListe'
import Tableau from './Tableau'

class App extends Component {


    addAdvert(element){
        getKaiAd({
            publisher: 'MON ID',
            app: 'ExCurrency',
            test:1, //1 for test, 0 for prod
            onerror: err => console.error('Custom catch:', err),
            onready: ad => {
                // Ad is ready to be displayed
                // custom event
                let button = document.getElementById(element)
                button.addEventListener('focus', function btnListener() {
                    button.removeEventListener('focus', btnListener)
                    // calling 'display' will display the ad
                    ad.call('display');
                })
            }
        });
    }


    componentDidMount(){
        this.addAdvert('ListeG');
    }

    render() {
      const {montant,
             montantRes,
             deviseListeG,
             deviseListeD,
             actualRate,
             dateUpdate,
             styleTab,
             tableauZero,
             nomDevises }= this.state

      return (
        <div>
            <Tableau styli={styleTab}
                     tableauO={tableauZero}
                     noms={nomDevises}
                     onKeyDown={this.handleKeydown}/>
        </div>
      )
  }
}
export default App;

我刚刚补充说: import {getKaiAd} from './Kaiads.v4.min.js'

我得到了:

Failed to compile.

./src/Kaiads.v4.min.js
  Line 1:695:   Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 1:792:   Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 1:901:   Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 1:1327:  Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 1:1385:  'getKaiAd' is not defined                                              no-undef
  Line 1:1395:  'getKaiAd' is not defined                                              no-undef
  Line 1:2229:  Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 1:2312:  Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 1:2369:  'getKaiAd' is not defined                                              no-undef
  Line 1:2420:  Expected an assignment or function call and instead saw an expression  no-unused-expressions
  Line 1:2430:  'getKaiAd' is not defined                                              no-undef
  Line 1:2466:  'getKaiAd' is not defined                                              no-undef

Search for the keywords to learn more about each error.

标签: javascriptreactjssdkkaios

解决方案


您需要在顶部导入它

import * as getKaiAd from "path/to/file"

或者

import {getKaiAd} from "path/to/file"

推荐阅读