简体   繁体   English

如何在与Webpack捆绑的Angular 1应用程序中使用Bootstrap

[英]How to use Bootstrap in Angular 1 app bundled with Webpack

I am trying to use Bootstrap in an Angular 1 app which relies on bundled JS and CSS. 我正在尝试在依赖捆绑的JS和CSS的Angular 1应用程序中使用Bootstrap。 Here is my current webpack.config.js: 这是我当前的webpack.config.js:

var webpack = require('webpack');

module.exports = {
  context: __dirname + '/src/assets/app',
  entry: {
    app: './app.module.js',
    vendor: [
      'angular',
      'jquery',
      'bootstrap/dist/css/bootstrap.css',
      'bootstrap'
    ]
  },
  output: {
    path: __dirname + '/src/assets/jsBundles',
    filename: 'app.bundle.js'
  },
  target: 'web',
  node: {
    fs: 'empty'
  },
  devtool: 'eval',
  plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      filename: 'vendor.bundle.js'
    }),
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery'
    })
  ],
  module: {
    rules: [
      {
        test: /\.html$/,
        use: ['html-loader']
      }, {
        test: /\.css$/,
        use: ['css-loader']
      }, {
        test: /\.woff($|\?)|\.woff2($|\?)|\.ttf($|\?)|\.eot($|\?)|\.svg($|\?)/,
        use: ['url-loader']
      }
    ]
  }
};

My Angular app successfully loads, but when I attempt to use any Bootstrap components or CSS they do not work. 我的Angular应用成功加载,但是当我尝试使用任何Bootstrap组件或CSS时,它们将无法工作。 No errors appear, but the Bootstrap styles are not applied and no Bootstrap JS like dropwdown or modal functionality works. 没有出现错误,但是没有应用Bootstrap样式,并且没有Dropsdown或模式功能之类的Bootstrap JS。 When I look in vendor.bundle.js I see all the Bootstrap JS, CSS, and fonts in there. 当我查看vendor.bundle.js时,在vendor.bundle.js看到了所有Bootstrap JS,CSS和字体。 Do I need to do something client-side to "load" Bootstrap? 我需要做一些客户端“加载” Bootstrap吗?

Try defined "window.jQuery": "jquery" as well in the ProvidePlugin . 尝试定义"window.jQuery": "jquery" ,以及在ProvidePlugin

For reference you can refer the webpack.config.js & webpack.common.config.js in link or can refer the starter kit for Angular 1.x and Webpack 作为参考,您可以参考链接中的webpack.config.jswebpack.common.config.js ,也可以参考Angular 1.x和Webpack的入门套件。

Since you are using html-loader and css-loader. 由于您使用的是html-loader和css-loader。 You can include css in the js file itself,just insert require('bootstrap/dist/css/bootstrap') or import(if es6) css file in the javascript file which is using the styles. 您可以在js文件本身中包含css,只需在使用样式的javascript文件中插入require('bootstrap/dist/css/bootstrap')import(if es6) css文件。 No need to provide an entry point to webpack for css 无需为CSS提供webpack的入口点

There are many possible ways to do above one is simple method,you can follow detailed webpack styles insertion here 以上是一种可能的方法,一种简单的方法,您可以按照此处详细介绍的webpack样式进行插入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM