0

在我的项目中,我使用 Routify、Bootstrp、Svelte 和 Webpack 来构建我的前端。如果我添加一个与Bootstrap 文档中提供的模式相同的模式和一个打开它的按钮,再次与文档几乎相同,单击该按钮绝对不会执行任何操作 - 没有模式打开,也没有错误记录在 JS 控制台中:

<!-- in a navbar, which renders correctly -->
<button class="btn btn-outline-primary" href="#" data-toggle="modal" data-target="#exampleModal">My Button</button>

<!-- ... -->

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                ...
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Save changes</button>
            </div>
        </div>
    </div>
</div>

为了解决这个问题,我尝试使用 jQuery 打开模型:

<script>
    import jquery from 'jquery';

    function openModal(){
        jquery('#exampleModal').modal('')
    }
</script>

<button class="btn btn-outline-primary" href="#" on:click={() => openModal()}>My Button</button>

<!-- The modal is the same as in the previous example. -->

就模式未打开而言,它会产生相同的行为,但在 JS 控制台中记录了以下错误:

Uncaught TypeError: jquery__WEBPACK_IMPORTED_MODULE_3___default()(...).modal is not a function

各种样板文件如下:

index.js:

import 'bootstrap/dist/css/bootstrap.min.css';
import 'bootstrap';
import 'jquery';
import '@fortawesome/fontawesome-free/css/all.css'

import App from './App.svelte';
const app = new App({
    target: document.body
});
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title><!-- removed --></title>
    <meta name="viewport" content="width=device-width, initial-scale=1"></head>
</head>
<body>
<script src="/bundle.js"></script>
</body>
</html>
{
  "name": /* removed */,
  "version": "0.0.0",
  "description": "",
  "scripts": {
    "buildRelease": "rm -rf dist && routify -b && webpack --mode=production && cp index.html dist/",
    "buildDebug": "rm -rf dist && routify -b && webpack --mode=development && cp index.html dist/"
  },
  "author": "",
  "devDependencies": {
    "file-loader": "^6.2.0",
    "url-loader": "^4.1.1",
    "style-loader": "^2.0.0",
    "css-loader": "^5.0.1",
    "svelte-loader": "^3.0.0",
    "webpack": "^5.20.0",
    "webpack-cli": "^4.5.0"
  },
  "dependencies": {
    "@fortawesome/fontawesome-free": "^5.15.3",
    "@popperjs/core": "^2.9.2",
    "@roxi/routify": "^2.11.3",
    "bootstrap": "^5.0.0",
    "jquery": "^3.6.0",
    "js-cookie": "^2.2.1",
    "popper.js": "^1.16.1",
    "svelte": "^3.32.1"
  },
  "routify": {
    "dynamicImports": false
  }
}

webpack.config.js:

const path = require('path');

module.exports = {
    devtool: 'source-map',
    entry: './src/index.js',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    resolve: {
        // see below for an explanation
        alias: {
            svelte: path.resolve('node_modules', 'svelte')
        },
        extensions: ['.mjs', '.js', '.svelte'],
        mainFields: ['svelte', 'browser', 'module', 'main']
    },
    module: {
        rules: [
            {
                test: /\.(html|svelte)$/,
                use: 'svelte-loader'
            },
            {
                test: /\.woff2?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                use: 'url-loader?limit=10000',
            },
            {
                // required to prevent errors from Svelte on Webpack 5+, omit on Webpack 4
                test: /node_modules\/svelte\/.*\.mjs$/,
                resolve: {
                    fullySpecified: false
                }
            },
            {
                test: /\.css$/i,
                use: ['style-loader', 'css-loader']
            },
            {
                test: /\.(ttf|eot|svg)(\?[\s\S]+)?$/,
                use: 'file-loader',
            }
        ]
    }
};
4

0 回答 0