angular2集成ng2-bootstrap

1、通過(guò)angular-cli創(chuàng)建項(xiàng)目

ng new my-app

2、加入bootstrap,ng2-bootstrap包

npm install ng2-bootstrap bootstrap@next --save

這里使用@next引用bootstrap4

3、引入bootstrap樣式文件

打開(kāi)src下的styles.css在其中加入以下代碼,樣式導(dǎo)入語(yǔ)法請(qǐng)看這里

@import url('../node_modules/bootstrap/dist/css/bootstrap.css');

在官方文檔里說(shuō)是在angular-cli.json下加入以下代碼:

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

但是經(jīng)過(guò)我測(cè)試,始終沒(méi)有效果,原文請(qǐng)看這里

我這里是angular-cli版本如下:

angular-cli: 1.0.0-beta.24

node: 6.9.1

os: darwin x64

@angular/common: 2.4.2

@angular/compiler: 2.4.2

@angular/core: 2.4.2

@angular/forms: 2.4.2

@angular/http: 2.4.2

@angular/platform-browser: 2.4.2

@angular/platform-browser-dynamic: 2.4.2

@angular/router: 3.4.2

@angular/compiler-cli: 2.4.2

4、編碼

打開(kāi)src/app/app.module.ts加入以下代碼:

import{AlertModule}from'ng2-bootstrap';

...

@NgModule({

...

imports: [AlertModule.forRoot(),...],

...

})

打開(kāi)src/app/app.component.html加入以下代碼:

<alert type="success">hello</alert>

5、運(yùn)行

ng serve

在瀏覽器里運(yùn)行http://localhost:4200查看效果


總結(jié):

1、當(dāng)時(shí)看著官方文檔,只想著在angular-cli.json中加入styles數(shù)組,但是一直都沒(méi)有效果,這是個(gè)坑啊,具體為何,不知。

2、在這里雖然能查看出效果,但是在safari中會(huì)報(bào)找不到bootstrap.css.map文件(404),通過(guò)ng build --prod,然后使用http-server運(yùn)行,問(wèn)題解決。

http-server用法,請(qǐng)看這里。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容