2

如何在 Angular 4 应用程序(使用 Angular CLI )中使用bootstrap-material-design

我只是styles.css像这样添加了css文件:

@import "~bootstrap-material-design/dist/css/bootstrap-material-design.min.css";

我可以访问 bootstrap-material-design 类,但某些元素(如输入)无法正常工作。

我想我必须添加.angular-cli.json文件jQuerybootstrap-material-design脚本,但这也不起作用。

正确的方法是什么?

从文档中,我需要通过将以下 javascript 添加到您的站点来初始化材料 javascript,但我不能。

$.material.init()
4

2 回答 2

1

感谢 Nano 的回答对我有帮助。

所以,我在 angular-cli.json 中添加了这样的样式和脚本:

    "styles": [
        "../node_modules/bootstrap/dist/css/bootstrap.min.css",
        "../node_modules/bootstrap-material-design/dist/css/bootstrap-material-design.min.css",
        "styles.css"
    ],
    "scripts": [
        "../node_modules/jquery/dist/jquery.min.js",
        "../node_modules/bootstrap/dist/js/bootstrap.min.js",
        "../node_modules/bootstrap-material-design/dist/js/material.min.js"
    ]

然后,我们必须初始化 bootstrap-material-design。在上下文初始化之后执行此操作很重要。所以,在 ngOnInit() 中对我来说是完美的。我在我的 app.component.ts 中这样做了,如下所示:

import { Component, OnInit } from '@angular/core';

declare let jQuery: any;

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
    ngOnInit() {
        // Init bootstrap-material-design
        jQuery.material.init();
    }
}

它就像我想要的那样工作。输入正常。

于 2017-06-19T15:36:20.910 回答
1

您已经通过运行安装了库

npm install --save bootstrap-material-design

您应该能够将所有 css 和 js 文件链接到您的项目。

为此,您应该在文件中链接此类angular-cli.json文件。

填写json的数组分别styles是css和scriptsjs的。

  "styles": [
    ...
    "../node_modules/bootstrap-material-design/sass/bootstrap-material-design.scss"
    ...
  ]

  ...

  "scripts": [
    "../node_modules/bootstrap-material-design/scripts/index.js"
  ]

希望这对你有用!

编辑:

如果您希望更轻松地使用 Material Design,您可以试试这个:

https://github.com/angular/material2

它也是官方的,与框架很好地集成。

于 2017-06-15T09:47:41.973 回答