【问题标题】:Using angular-material-design in Angular 4 app (+ Angular CLI)在 Angular 4 应用程序(+ Angular CLI)中使用 angular-material-design
【发布时间】:2017-11-17 17:25:12
【问题描述】:

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

我刚刚在styles.css 中添加了这样的 css 文件:

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

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

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

正确的方法是什么?

在 Docs 中,我需要通过将以下 javascript 添加到您的网站来初始化材料 javascript,但我不能。

$.material.init()

【问题讨论】:

    标签: jquery angular typescript angular-cli bootstrap-material-design


    【解决方案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();
        }
    }
    

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

    【讨论】:

    • 对于 bootstrap 4,他们要求添加 $('body').bootstrapMaterialDesign();
    【解决方案2】:

    你已经通过运行安装了库

    npm install --save bootstrap-material-design
    

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

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

    要填写json的数组分别是css的styles和js的scripts

      "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

    也是官方的,和框架很好的集成。

    【讨论】:

    • 嗨纳米!谢谢您的回答。你把这个调用放在哪里了:$.material.init()?为什么要导入 /scripts/index.js 而不是 /dist/js/material.js?
    • 你可以从 index.html 页面的 script 标签中调用它!
    • 你导入 /scripts/index.js 因为它是开发版本而不是编译版本。
    猜你喜欢
    • 2017-06-28
    • 2017-09-13
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 2017-12-17
    • 2017-06-18
    • 2019-10-04
    • 1970-01-01
    相关资源
    最近更新 更多