2017-08-12 147 views
58

我剛剛創建了一個全新的項目
npm install [email protected] jquery popper.js --save
,改變了.angular-cli.json的相關部件如下Bootstrap4依賴PopperJs拋出錯誤對角

"styles": [ 
    "../node_modules/bootstrap/dist/css/bootstrap.css" 
    ], 
    "scripts": [ 
    "../node_modules/jquery/dist/jquery.js", 
    "../node_modules/popper.js/dist/popper.js", 
    "../node_modules/bootstrap/dist/js/bootstrap.js" 
    ], 

然而,收到以下錯誤

10:2287 Uncaught SyntaxError: Unexpected token export 
    at eval (<anonymous>) 
    at webpackJsonp.../../../../script-loader/addScript.js.module.exports (addScript.js:9) 
    at Object.../../../../script-loader/index.js!../../../../popper.js/dist/popper.js (popper.js?4b43:1) 
    at __webpack_require__ (bootstrap 4403042439558687cdd6:54) 
    at Object.2 (scripts.bundle.js:66) 
    at __webpack_require__ (bootstrap 4403042439558687cdd6:54) 
    at webpackJsonpCallback (bootstrap 4403042439558687cdd6:25) 
    at scripts.bundle.js:1 

任何想法如何解決它?

在此先感謝...

+0

奇怪,我找到了它爲我工作。你使用最新的cli嗎?你可以嘗試重新安裝node_modules – LLai

+1

錯誤必須在其他地方 – brijmcq

+0

@LLai'@ angular/cli:1.3.0 node:6.11.2 npm:5.3.0'你是否更改過**中的部分:angular-cli.json * *如果你沒有,你將不會在控制檯中看到錯誤。 – cilerler

回答

133

如果你看一下引導頁面(http://getbootstrap.com/)中可以看出,他們進口了以下內容:

https://code.jquery.com/jquery-3.2.1.slim.min.js 
https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js 
https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js 

注意命名:jquery。 苗條 .min.js,UMD /popper.min.js

所以我用我的.angular-cli.json如下:

 "styles": [ 
     "../node_modules/bootstrap/dist/css/bootstrap.css" 
     ], 
     "scripts": [ 
     "../node_modules/jquery/dist/jquery.slim.min.js", 
     "../node_modules/popper.js/dist/umd/popper.min.js", 
     "../node_modules/bootstrap/dist/js/bootstrap.min.js" 
     ], 

之後,它似乎現在的工作。

+0

你在jQuery包的packages.json中有什麼?在我的node_modules jquery文件夾中,我沒有jquery.slim.min.js。其實只是發現它在jquery 3.2.1 – lankyplanks

+2

我的package.json看起來如下jquery: '「jquery」:「^ 3.2.1」' 我已經testet與完整的jquery版本,它似乎工作也。 只有** umd **/popper.min.js似乎是必需的。 –

+0

正如你所說......「jquery」:「^ 3.2.1」效果很好 – lankyplanks

0

我有同樣的問題,並吸...但如果你直接導入上的index.html頭部標籤,就像bootrstrap說的,不與NPM ......你獲得好的結果。那不會拋出問題......但只是一個補丁。但是,如果用戶沒有互聯網,是另一個歷史。

1

我可以看到很多人都在努力添加並正確包含Bootstrap 4依賴項(jQuery,popper.js等)。但是以https://ng-bootstrap.github.io的形式有一個更簡單的解決方案。

ng-bootstrap提供原生角度指令從頭開始編寫。陽性結果是: *您並不需要包括和擔心jQuery的,popper.js等 *指令提供的API,「意義」的角度世界

對於任何人試圖利用引導4 .beta與角 - NG-引導剛剛發佈了其第一個測試版是與引導4.beta CSS完全兼容

+0

如果您使用的是Angular,但是如果您使用的是AngularJS,則會更容易... –

+1

嗯,問題是關於Angular和angular-cli所以不知道爲什麼AngularJS會被帶到這裏...... –

19

我有同樣的問題。我的解決方案是而不是導入dist文件夾(./node_modules/popper.js/dist/popper.js),而是umd文件夾(./node_modules/popper.js/dist/ udd/popper。 JS)。如果您獲得js文件的最小或普通版本,則無關緊要。