2016-06-13 74 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