2017-10-18 66 views
0

我遵循quick start,並且創建了一個工作好的世界webpart。Sharepoint SPFX - 外部javascript加載問題

我想包括Twitter的插件,所以我跟着上添加外部JavaScript指導,this從Git的樞紐連接

我以下內容添加到我的config.json

"externals": { 
    "widgets":"https://platform.twitter.com/widgets.js" 
}, 

和我用進口

import 'widgets'; 

不過,我得到以下錯誤

***Failed to load component "ab3668f3-39a2-4b40-a307-a1bea4023df9" (TwitterWebPart). 
Original error: ***Failed to load URL 'https://platform.twitter.com/widgets.js' for resource 'widgets' in component 'ab3668f3-39a2-4b40-a307-a1bea4023df9' (TwitterWebPart). There was a network problem. 
This may be a problem with a HTTPS certificate. Make sure you have the right certificate. 

url https://platform.twitter.com/widgets.js工作正常。

回答

1

看起來像這裏有2個問題。

This may be a problem with a HTTPS certificate. - 這個錯誤看起來是來自twitter的結尾,我們對此無能爲力。其次,widgets.js不是AMD。所以,你需要加載它有點不同。

參考 - Loading a non-AMD module

要解決這些問題,我下載到本地的文件https://platform.twitter.com/widgets.js,並在另一個文件夾添加它。

在那之後,我修改了config.json如下:

"externals": {  
     "widgets": { 
      "path": "scripts/widgets.js", 
      "globalName": "twttr" 
     } 
    }, 

在這裏,我創建了一個名爲scripts夾在所述溶液的根,並加入JS文件那裏。

修改您的導入語句,並添加如下替換:

require("widgets"); 

隨着內容注入到頁面中,你的Twitter Feed就不會顯示,添加如下代碼來加載您的tweets

public constructor() { 
    super(); 
    let w= require("widgets"); 
    setTimeout(() => w.widgets.load(), 0); 
} 

我的文件夾結構如下:

enter image description here

現在,運行gulp服務,它會正常工作。