2016-07-04 116 views
2

直接從https://angular.io/docs/ts/latest/guide/forms.html角2不會導入CSS引導

讓我們添加的樣式表。

打開應用程序根文件夾一個終端窗口和輸入命令:

NPM安裝引導--save

打開index.html並添加以下連結的頭部。

鏈路的rel = 「樣式表的」 href = 「node_modules /引導/ DIST/CSS/bootstrap.min.css」>

然而,我不斷收到404

GET http://localhost:4200/node_modules/bootstrap/dist/css/bootstrap.min.css

它的作品,當我添加在線地址的腳本,但爲什麼角無法找到我的文件的路徑?

+0

您可以檢查是否存在的CSS在文件系統上的那個位置。你使用角度cli來建立?如果是,那麼您需要將其複製到供應商並調整路徑 –

+0

是的,該文件在那裏。通過添加到供應商你的意思是vendorNpmFiles?我在那裏添加了這行'bootstrap/**/*',它仍然不起作用。 –

+0

是的,現在檢查文件是否在dist/vendor中。如果是,那就改鏈接鏈接rel =「stylesheet」href =「vendor/bootstrap/dist/css/bootstrap.min.css」> –

回答

5

index.htmlnode_modules位於同一目錄時,它正在工作。

要添加引導角CLI把

'bootstrap/dist/**/*.+(js|css.map|css|eot|svg|ttf|woff|woff2)'

angular-cli-build.js和運行ng build

+0

謝謝!這就是訣竅:) –

0

您正在使用的構建角度CLI因此你需要在更新vendorNpmFiles複製引導到供應商目錄angularcli-build.js

bootstrap/**/*.* 

現在您需要更新的index.html的鏈接,從銷售商

< link rel="stylesheet" href="vendor/bootstrap/dist/css/bootstrap.min.css"> 
3

挑CSS如果使用角-CLI的過渡的WebPack後,添加

"../node_modules/bootstrap/dist/css/bootstrap.css" 

styles陣列中angular-cli.json像:

"styles": [ 
    "styles.css", 
    "../node_modules/bootstrap/dist/css/bootstrap.css" 
], 

如果您還使用bootstrap javascript,請將這些文件添加到scripts陣列中。

+0

建議將Bootstrap CSS應用於應用[0] .styles; E.I. 「樣式」:[ 「../node_modules/bootstrap/dist/css/bootstrap.css」, 「styles.css」 ],參見https://www.npmjs.com/package/angular-cli –

+0

@ MartinW將它添加到angular-cli.json或僅將其導入sass/scss文件(如果使用的話)之間有什麼區別? – Todilo

+0

不知道,爲什麼沒有人贊成這個?這是最簡單的方法。但我確實必須重做「ng serve」才能獲得更改。 – Plankton

12

如果您使用角CLI,你可以去root/styles.css,並添加此行

@import "../node_modules/bootstrap/dist/css/bootstrap.min.css"; 

或者去.angular-cli.json和修改styles

"styles": [ 
"../node_modules/bootstrap/dist/css/bootstrap.min.css", 
"styles.css" 
] 
+0

這個答案在一年後仍然有效。做得好! – EdoB

+0

是的,這工作....謝謝,如果你這樣做,你不需要添加index.html。 – Buminda