Uncaught ReferenceError: Popper is not defined - with Bootstrap 4 and Webpack 3.8.1
Uncaught ReferenceError: Popper is not defined - with Bootstrap 4 and Webpack 3.8.1
我在 .NET Core 2.0 中使用 Visual Studio 2017 Angular 模板。该模板有许多我已经更新的旧依赖项,包括 Angular 5.0.0。 运行 带有 Bootstrap 3.x.x 的原始模板有效。但是,使用 Bootstrap 4.0.0-beta.2 会导致以下错误:
Uncaught ReferenceError: Popper is not defined
我已尽我所能按照此处的说明进行操作:
http://getbootstrap.com/docs/4.0/getting-started/webpack/
我看过这个问题:
根据那里列出的解决方案,我必须在 bootstrap 之前注册 popper 或使用 boostrap 的最小版本,因为它已经带有 popper。但是,我正在使用 webpack 2.5.1(我刚刚在学习),我不知道如何在 webpack.config.js 文件中实现这些解决方案中的任何一个作为模板的一部分提供,包括由 boostrap-webpack 文档调用的修改。
webpack.config.js
const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const AotPlugin = require('@ngtools/webpack').AotPlugin;
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;
module.exports = (env) => {
// Configuration in common to both client-side and server-side bundles
const isDevBuild = !(env && env.prod);
const sharedConfig = {
stats: { modules: false },
context: __dirname,
resolve: { extensions: ['.js', '.ts'] },
output: {
filename: '[name].js',
publicPath: 'dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
},
module: {
rules: [
{ test: /\.ts$/, include: /ClientApp/, use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader'] : '@ngtools/webpack' },
{ test: /\.html$/, use: 'html-loader?minimize=false' },
{ test: /\.css$/, use: ['to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize'] },
{ test: /\.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=25000' }
]
},
plugins: [new CheckerPlugin()]
};
// Configuration for client-side bundle suitable for running in browsers
const clientBundleOutputDir = './wwwroot/dist';
const clientBundleConfig = merge(sharedConfig, {
entry: { 'main-client': './ClientApp/boot.browser.ts' },
output: { path: path.join(__dirname, clientBundleOutputDir) },
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./wwwroot/dist/vendor-manifest.json')
}),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
Popper: ['popper.js', 'default'],
// In case you imported plugins individually, you must also require them here:
//Util: "exports-loader?Util!bootstrap/js/dist/util",
//Dropdown: "exports-loader?Dropdown!bootstrap/js/dist/dropdown"
})
].concat(isDevBuild ? [
// Plugins that apply in development builds only
new webpack.SourceMapDevToolPlugin({
filename: '[file].map', // Remove this line if you prefer inline source maps
moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
})
] : [
// Plugins that apply in production builds only
new webpack.optimize.UglifyJsPlugin(),
new AotPlugin({
tsConfigPath: './tsconfig.json',
entryModule: path.join(__dirname, 'ClientApp/app/app.module.browser#AppModule'),
exclude: ['./**/*.server.ts']
})
])
});
// Configuration for server-side (prerendering) bundle suitable for running in Node
const serverBundleConfig = merge(sharedConfig, {
resolve: { mainFields: ['main'] },
entry: { 'main-server': './ClientApp/boot.server.ts' },
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./ClientApp/dist/vendor-manifest.json'),
sourceType: 'commonjs2',
name: './vendor'
})
].concat(isDevBuild ? [] : [
// Plugins that apply in production builds only
new AotPlugin({
tsConfigPath: './tsconfig.json',
entryModule: path.join(__dirname, 'ClientApp/app/app.module.server#AppModule'),
exclude: ['./**/*.browser.ts']
})
]),
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, './ClientApp/dist')
},
target: 'node',
devtool: 'inline-source-map'
});
return [clientBundleConfig, serverBundleConfig];
};
此外,这是作为模板的一部分提供的主要入口点文件 boot.browser.ts:
import 'reflect-metadata';
import 'zone.js';
import 'bootstrap';
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module.browser';
if (module.hot) {
module.hot.accept();
module.hot.dispose(() => {
// Before restarting the app, we create a new root element and dispose the old one
const oldRootElem = document.querySelector('app');
const newRootElem = document.createElement('app');
oldRootElem!.parentNode!.insertBefore(newRootElem, oldRootElem);
modulePromise.then(appModule => appModule.destroy());
});
} else {
enableProdMode();
}
// Note: @ng-tools/webpack looks for the following expression when performing production
// builds. Don't change how this line looks, otherwise you may break tree-shaking.
const modulePromise = platformBrowserDynamic().bootstrapModule(AppModule);
尝试在 bootstrap 之前而不是在
之后包括 Popper.js
"../node_modules/popper.js/dist/umd/popper.min.js",
"../node_modules/bootstrap/dist/js/bootstrap.min.js"
我终于找到了解决错误的方法。我在主入口点 boot.browser.ts 文件中使用了以下几行:
import * as $ from 'jquery';
//These two lines didn't remove the error
//import Popper from 'popper.js';
//import 'bootstrap';
//This one did remove the error. The bundle version of bootstrap includes popper in the correct order (according to the docs)
import 'bootstrap/dist/js/bootstrap.bundle.js';
有趣的是,bootstrap github (https://github.com/twbs/bootstrap/issues/24648) 中的建议代码是将此行用于 jquery 导入:
import $ from 'jquery'
而不是
import * as $ from 'jquery'
但是前者导致了这个错误:
Could not find a declaration file for module 'jquery'
但是,我不知道这两条线有什么区别
我安装了一个特定版本的 popper.js 我知道它正在使用 bootstrap 4.0.0-beta,就像这样:
npm install popper.js@1.12.5 --save-dev
然后我只需要像这样将它添加到我的 webpack.config.vendor.js 中:
new webpack.ProvidePlugin(
{
$: 'jquery',
jQuery: 'jquery', // }, Maps these identifiers to the jQuery package (because Bootstrap expects it to be a global variable)
jquery: 'jquery',
'window.jQuery': 'jquery',
Popper: 'popper.js/dist/umd/popper.js'
}
),
而且我不再收到 Uncaught ReferenceError: Popper is not defined 错误。
我没有更改代码,只更改了 webpack 和 npm。
我在 .NET Core 2.0 中使用 Visual Studio 2017 Angular 模板。该模板有许多我已经更新的旧依赖项,包括 Angular 5.0.0。 运行 带有 Bootstrap 3.x.x 的原始模板有效。但是,使用 Bootstrap 4.0.0-beta.2 会导致以下错误:
Uncaught ReferenceError: Popper is not defined
我已尽我所能按照此处的说明进行操作:
http://getbootstrap.com/docs/4.0/getting-started/webpack/
我看过这个问题:
根据那里列出的解决方案,我必须在 bootstrap 之前注册 popper 或使用 boostrap 的最小版本,因为它已经带有 popper。但是,我正在使用 webpack 2.5.1(我刚刚在学习),我不知道如何在 webpack.config.js 文件中实现这些解决方案中的任何一个作为模板的一部分提供,包括由 boostrap-webpack 文档调用的修改。
webpack.config.js
const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const AotPlugin = require('@ngtools/webpack').AotPlugin;
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;
module.exports = (env) => {
// Configuration in common to both client-side and server-side bundles
const isDevBuild = !(env && env.prod);
const sharedConfig = {
stats: { modules: false },
context: __dirname,
resolve: { extensions: ['.js', '.ts'] },
output: {
filename: '[name].js',
publicPath: 'dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
},
module: {
rules: [
{ test: /\.ts$/, include: /ClientApp/, use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader'] : '@ngtools/webpack' },
{ test: /\.html$/, use: 'html-loader?minimize=false' },
{ test: /\.css$/, use: ['to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize'] },
{ test: /\.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=25000' }
]
},
plugins: [new CheckerPlugin()]
};
// Configuration for client-side bundle suitable for running in browsers
const clientBundleOutputDir = './wwwroot/dist';
const clientBundleConfig = merge(sharedConfig, {
entry: { 'main-client': './ClientApp/boot.browser.ts' },
output: { path: path.join(__dirname, clientBundleOutputDir) },
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./wwwroot/dist/vendor-manifest.json')
}),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
Popper: ['popper.js', 'default'],
// In case you imported plugins individually, you must also require them here:
//Util: "exports-loader?Util!bootstrap/js/dist/util",
//Dropdown: "exports-loader?Dropdown!bootstrap/js/dist/dropdown"
})
].concat(isDevBuild ? [
// Plugins that apply in development builds only
new webpack.SourceMapDevToolPlugin({
filename: '[file].map', // Remove this line if you prefer inline source maps
moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
})
] : [
// Plugins that apply in production builds only
new webpack.optimize.UglifyJsPlugin(),
new AotPlugin({
tsConfigPath: './tsconfig.json',
entryModule: path.join(__dirname, 'ClientApp/app/app.module.browser#AppModule'),
exclude: ['./**/*.server.ts']
})
])
});
// Configuration for server-side (prerendering) bundle suitable for running in Node
const serverBundleConfig = merge(sharedConfig, {
resolve: { mainFields: ['main'] },
entry: { 'main-server': './ClientApp/boot.server.ts' },
plugins: [
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./ClientApp/dist/vendor-manifest.json'),
sourceType: 'commonjs2',
name: './vendor'
})
].concat(isDevBuild ? [] : [
// Plugins that apply in production builds only
new AotPlugin({
tsConfigPath: './tsconfig.json',
entryModule: path.join(__dirname, 'ClientApp/app/app.module.server#AppModule'),
exclude: ['./**/*.browser.ts']
})
]),
output: {
libraryTarget: 'commonjs',
path: path.join(__dirname, './ClientApp/dist')
},
target: 'node',
devtool: 'inline-source-map'
});
return [clientBundleConfig, serverBundleConfig];
};
此外,这是作为模板的一部分提供的主要入口点文件 boot.browser.ts:
import 'reflect-metadata';
import 'zone.js';
import 'bootstrap';
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module.browser';
if (module.hot) {
module.hot.accept();
module.hot.dispose(() => {
// Before restarting the app, we create a new root element and dispose the old one
const oldRootElem = document.querySelector('app');
const newRootElem = document.createElement('app');
oldRootElem!.parentNode!.insertBefore(newRootElem, oldRootElem);
modulePromise.then(appModule => appModule.destroy());
});
} else {
enableProdMode();
}
// Note: @ng-tools/webpack looks for the following expression when performing production
// builds. Don't change how this line looks, otherwise you may break tree-shaking.
const modulePromise = platformBrowserDynamic().bootstrapModule(AppModule);
尝试在 bootstrap 之前而不是在
之后包括 Popper.js "../node_modules/popper.js/dist/umd/popper.min.js",
"../node_modules/bootstrap/dist/js/bootstrap.min.js"
我终于找到了解决错误的方法。我在主入口点 boot.browser.ts 文件中使用了以下几行:
import * as $ from 'jquery';
//These two lines didn't remove the error
//import Popper from 'popper.js';
//import 'bootstrap';
//This one did remove the error. The bundle version of bootstrap includes popper in the correct order (according to the docs)
import 'bootstrap/dist/js/bootstrap.bundle.js';
有趣的是,bootstrap github (https://github.com/twbs/bootstrap/issues/24648) 中的建议代码是将此行用于 jquery 导入:
import $ from 'jquery'
而不是
import * as $ from 'jquery'
但是前者导致了这个错误:
Could not find a declaration file for module 'jquery'
但是,我不知道这两条线有什么区别
我安装了一个特定版本的 popper.js 我知道它正在使用 bootstrap 4.0.0-beta,就像这样:
npm install popper.js@1.12.5 --save-dev
然后我只需要像这样将它添加到我的 webpack.config.vendor.js 中:
new webpack.ProvidePlugin(
{
$: 'jquery',
jQuery: 'jquery', // }, Maps these identifiers to the jQuery package (because Bootstrap expects it to be a global variable)
jquery: 'jquery',
'window.jQuery': 'jquery',
Popper: 'popper.js/dist/umd/popper.js'
}
),
而且我不再收到 Uncaught ReferenceError: Popper is not defined 错误。
我没有更改代码,只更改了 webpack 和 npm。