2016-06-13 75 views
0

我想在Meteor的反应组件中包含一个外部脚本。 我已经尝试只是放置脚本标记在我的组件,像这样:在React/Meteor中使用外部脚本

TheLounge = React.createClass({ 
    render() { 
    return (
     <div> 
     <div className="main"> 
      {/* Uberflip Embedded Hub (Use this to generate the frame below) */} 
      <script>var ufh_top_offset = 0, ufh_bottom_offset = 0;</script> 
      <script type="text/javascript" src="https://thelounge.tullyluxurytravel.com/hubsFront/embed_iframe?u=https%3A%2F%2Fthelounge.tullyluxurytravel.com%2Fh%2F%3Fembedded%3D1%26offsetTop%3D0%26offsetBottom%3D0%26hideHeader%3D1%26hideBanner%3D0%26hideFooter%3D1%26hidePriNav%3D0%26hideSecNav%3D0%26linkBreakOut%3D0"></script> 
      {/* /End Uberflip Embedded Hub */} 

     </div> 
     <Footer /> 
     </div> 
    ); 
    }, 
}); 

这使得精细首次加载页面(通过另一个页面或刷新)。但是,当导航到另一页并返回时,脚本不会再次加载。

到目前为止,我已经尝试以下操作:

  • 注射在componentDidMount和componentDidUpdate脚本如下所示:

在componentDidMount和componentDidUpdate:

var script = document.createElement("script"); 
script.type = "text/javascript"; 
script.src = url; 
// Tested the line below with body, head, and a div with specific id. 
document.getElementsByTagName("body")[0].appendChild(script); 

我我也试过使用这些软件包:

然而,与上述我的四种方法得到的错误:

Error: Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened. 

我知道这是因为剧本我试图加载使用document.write();

我没有办法尝试,我很欣赏新的想法,以便如何将此脚本包含到React组件中。

回答

1

标签或模板不被执行的流星,它们被解析,然后由流星的模板系统来处理。尝试jQuery getScript功能:

componentWillMount() { 
    $.getScript('https://thelounge.tullyluxurytravel.com/hubsFront/embed_iframe?u=https%3A%2F%2Fthelounge.tullyluxurytravel.com%2Fh%2F%3Fembedded%3D1%26offsetTop%3D0%26offsetBottom%3D0%26hideHeader%3D1%26hideBanner%3D0%26hideFooter%3D1%26hidePriNav%3D0%26hideSecNav%3D0%26linkBreakOut%3D0'); 
} 
+0

请注意,这将永远不会缓存脚本 – Gwened