gpt4 book ai didi

jquery - 使用 parcel.js 导入 select2

转载 作者:行者123 更新时间:2023-12-05 06:53:00 25 4
gpt4 key购买 nike

请原谅我缺乏知识,因为我对网络开发还很陌生。

我正在尝试捆绑所有第 3 方包,其中包括 select2 等。该 bundle 适用于其他包,如 jquery、bootstrap、popper 等,但 select2 未初始化或其他。也许是加载顺序问题?

调用 $('select').select2() 导致未捕获的类型错误。通过 cdn 或手动下载单独加载它是有效的,所以 parcel 似乎没有做正确的事情?

这几乎是一个非常“样板”的设置,因此很容易复制,但这是我目前正在尝试的:

File structure:
- node_modules
- index.html
- .sassrc # sass configuration
- assets/ # front end assets root
- scss/ # Place for all styles
- site.scss
- js/ # Place for all scripts
- site.js
- bundle.js # Entry point for output bundle
// bundle.js
import './scss/site.scss';
import $ from 'jquery';
window.$ = window.jQuery = $;
import 'bootstrap';
import 'select2';
/* site.css */
@import "bootstrap/scss/bootstrap";
@import "select2/dist/css/select2";
@import "@ttskch/select2-bootstrap4-theme/dist/select2-bootstrap4";
<!-- index.html -->
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="wwwroot/dist/bundle.css">
<script src="wwwroot/dist/bundle.js"></script>
</head>

最后

// package.js to show dependencies and the build script
{
"name": "SideNav",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"build": "parcel build assets/bundle.js --out-dir wwwroot/dist/"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@ttskch/select2-bootstrap4-theme": "^1.3.5",
"bootstrap": "^4.6.0",
"jquery": "^3.5.1",
"popper.js": "^1.16.1",
"select2": "^4.0.13"
},
"devDependencies": {
"parcel-bundler": "^1.12.4",
"sass": "^1.32.5"
}
}

提前感谢大家的帮助。

最佳答案

对我来说,select2 是这样工作的:

import $ from "jquery";
import select2 from "select2";

select2($);
window.$ = $;

关于jquery - 使用 parcel.js 导入 select2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65852956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com