如何在ReactJS应用中插入第三方库

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

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

该怎么做! (js library file

这是我的简化代码,我们可以看到第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.
iCMS 回答:如何在ReactJS应用中插入第三方库

您需要像在顶部那样导入

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

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

解决方案是:

首先,将其添加到index.html

<script type="text/javascript" src="libs/kaiads.v4.min.js" defer></script>

然后,像这样调用函数:window.getKaiAd

本文链接:https://www.f2er.com/1739589.html

大家都在问