简体   繁体   English

React - 使用来自 CDN 的外部 js

[英]React - Using external js from CDN

Sorry, I put this again since the old post got merged into some post that doesn't relate to my question ... I'm new to React and trying to convert a php website into react components.抱歉,我再次提出这个问题,因为旧帖子被合并到一些与我的问题无关的帖子中......我是 React 的新手,正在尝试将 php 网站转换为 React 组件。 However, in old website there are some function in pure jquery and from CDSNJS.但是,在旧网站中,有一些纯 jquery 和 CDSNJS 的功能。 The external javascripts function are not binding well with my component and I cannot figure out how to.外部 javascripts 函数与我的组件绑定得不好,我不知道如何绑定。 Please can anyone give me some advice.请任何人都可以给我一些建议。

Case 1: I got a an external function like this:案例 1:我有一个这样的外部函数:

;(function ($) {
    /* Global variables */
    var prty, flickr;
    
    $.fn.flickrGallery = function(flickrID) {
        //Defaults settings  
        var opts = $.extend({}, {
            Key:prty.settings["Key"], 
            Secret:prty.settings["Secret"], 
            User:prty.settings["User"], 
            PhotoSet:flickrID, 
            Speed:400, 
            navigation:1,
            keyboard:1,numberEl:1 });  
         //Setup
         prty.Setup($(this), opts); 
    }; //End FN    
prty = { 
.... Internal code
};
})(jQuery);

And this is my component's code:这是我的组件的代码:

async componentDidMount() {

    //$('#gallery').flickrGallery(2); // Not work

    //const el = ReactDOM.findDOMNode(this.display); Not work
    //$(el).vectorMap({map: 'world_mill_en'});
    //$(el).flickrGallery(2); 

    //const el = ReactDOM.findDOMNode(this.domRef); Not work
    //window.$('#addSupModal').modal('show')
    //$(el).flickrGallery(2);

    window.$(this.productIntro).flickrGallery(2); Not work

} }

Every time I run, an error like this appears:每次运行都会出现这样的错误:

Unhandled Rejection (TypeError): window.$(...).flickrGallery is not a function

Case 2:案例2:

Beside the case above, I'm also using a lib from CDNJS除了上面的例子,我还使用了 CDNJS 的库

<!-- jQuery 1.8 or later, 33 KB -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<!-- Fotorama from CDNJS, 19 KB -->
<link  href="https://cdnjs.cloudflare.com/ajax/libs/fotorama/4.6.4/fotorama.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/fotorama/4.6.4/fotorama.js"></script>

I have tried including these links into index.html but the same error as above happens when I run ... Please help我已经尝试将这些链接包含到 index.html 中,但是当我运行时会发生与上述相同的错误......请帮忙

Try to access the flickrGallery function via window.jQuery instead of window.$ .尝试通过window.jQuery而不是window.$访问flickrGallery函数。

The plugin add the flickrGallery function to jQuery .该插件将flickrGallery函数添加到jQuery In most of the time, jQuery should be the same as $ .在大多数情况下, jQuery应该与$相同。 However, in some cases, multiple version of jQuery are loaded and jQuery may no longer be equals to $ .但是,在某些情况下,加载了多个版本的 jQuery 并且jQuery可能不再等于$


The following suggestions were told to be not solving the problem.以下建议被告知不能解决问题。 I will keep them below in case someone find it useful.如果有人觉得有用,我会将它们保留在下面。


It looks like your react component script is executed and rendered before the external scripts is being executed.看起来您的 React 组件脚本是在执行外部脚本之前执行和呈现的。 There are many causes to it and one of the reasons is the ordering of your script tags.造成这种情况的原因有很多,其中一个原因是脚本标签的顺序。

In the document head , make sure the ordering of your script tag is as follow.在文档head ,确保脚本标签的顺序如下。

<script src="path/to/external/library.js"></script>
<script src="path/to/your/react/script.js"></script>

These script tags do not have the defer nor async attribute, suggesting they should be downloaded, parsed and executed in order.这些脚本标签没有deferasync属性,建议按顺序下载、解析和执行。 The external script is executed first.首先执行外部脚本。

In other cases, where there are defer , async , etc. attributes are in the script tag, you can understand the order of execution by reading this cheat sheet .在其他情况下,脚本标签中有deferasync等属性,您可以通过阅读此备忘单了解执行顺序。


If you are using plugin which adds its functionality to some global variable, your bundler may tree-shake it away because it does not detect any usage of its exports and thinks the module is not needed.如果您正在使用将其功能添加到某个全局变量的插件,您的捆绑器可能会将其删除,因为它没有检测到其导出的任何用法并认为不需要该模块。 In this case, you have to add side effect import ( Doc ).在这种情况下,您必须添加副作用导入( Doc )。

In your entry module, add this at the very top:在您的入口模块中,在最顶部添加:

import 'some-jquery-plugin'

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM