如何在Angular (v15) 中传统地使用 Bootstrap (v5)。

huangapple go评论69阅读模式
英文:

How to use bootstrap (v5) conventionally in Angular (v15)

问题

我已经使用以下命令在Angular中设置了Bootstrap,基于[ng-bootstrap.github.io文档][1]:
```shell
ng new my-ng-bootstrap-app # 创建Angular应用。选择路由和SCSS选项
ng add @ng-bootstrap/ng-bootstrap # 添加Bootstrap(请参阅本答案底部有关如何将Bootstrap添加到应用程序的说明)
ng generate component my-flex # 创建一个新组件以测试Bootstrap的弹性样式
ng serve # 运行Angular服务器

(我在这里详细说明了完整的设置方法 https://stackoverflow.com/a/75039099/11664580

这使我可以使用Bootstrap小部件,但当我尝试以传统方式使用Bootstrap时,什么也不会发生 - 例如:

      <button type="button" class="btn btn-primary">
        我的按钮
      </button>

上面的代码不会应用任何样式,尽管我已按照ng-bootstrap的说明进行了设置。

将Bootstrap样式应用于我的Angular应用的最佳方法是什么?我通过简单地在app.component.html中添加一个带有hreflink元素来使其工作:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

或者我应该遵循其他建议,通过npm install bootstrap进行安装?


<details>
<summary>英文:</summary>

I&#39;ve set up Bootstrap with Angular using the following commands, based on the [ng-bootstrap.github.io docs][1]:
```shell
ng new my-ng-bootstrap-app # Create angular app. Choose routing and SCSS options
ng add @ng-bootstrap/ng-bootstrap # add bootstrap (see notes at bottom of this answer for how bootstrap is added to app)
ng generate component my-flex # Create a new component to test bootstrap&#39;s flex styles
ng serve # Run the Angular server

(I've detailed the full set-up method here https://stackoverflow.com/a/75039099/11664580)

This allows me to use bootstrap widgets, but when I try to use Bootstrap conventionally, nothing happens - eg:

      &lt;button type=&quot;button&quot; class=&quot;btn btn-primary&quot;&gt;
        My Button
      &lt;/button&gt;

The above doesn't apply any styles, even though I have set up as per the ng-bootstrap instructions.

What's the best way to apply bootstrap styles to my Angular app? I've got it working by simply adding a link element with the href to my app.component.html:

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css&quot; integrity=&quot;sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm&quot; crossorigin=&quot;anonymous&quot;&gt;

Or should I follow other guidance and install with npm install bootstrap?

答案1

得分: 1

如果你正在使用```npm install bootstrap```进行安装,
然后在```angular.json```文件中搜索

styles 并添加路径,如下所示...

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


你可以从```node_module > bootstrap > dist > css > bootstrap.min.css```复制相关路径,或者你可以复制```node_module > bootstrap > dist > css > bootstrap.css```。

在```angular.json```中有2个styles,在两个styles中都添加路径。

然后应该可以正常工作,我猜。
英文:

if you are going with npm install bootstrap

then after installing in angular.json file search for

styles and add the path like...

&quot;styles&quot;: [
  &quot;node_modules/bootstrap/dist/css/bootstrap.min.css&quot;,
  &quot;src/styles.css&quot;
]

you can copy the relavent path from node_module &gt; bootstrap &gt; dist &gt; css &gt; bootstrap.min.css or you can copy node_module &gt; bootstrap &gt; dist &gt; css &gt; bootstrap.css

in angular.json there are 2 styles, given path in both styles.

then it should work perfectly I guess.

huangapple
  • 本文由 发表于 2023年1月9日 18:10:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定