微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Svelte 和 Routify 时不会出现 Bootstrap 模态

如何解决使用 Svelte 和 Routify 时不会出现 Bootstrap 模态

在我的项目中,我使用 Routify、Bootstrp、svelte 和 Webpack 来构建我的前端。如果我添加一个Bootstrap's documentation 中提供的模式相同的模式和一个打开它的按钮,同样与文档几乎相同,点击按钮绝对没有任何作用——没有模式打开,也没有错误记录在 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',}
        ]
    }
};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。