Svelte Fusion with Bootstrap Harmony

Lately, I've been delving into Svelte and finding it quite enjoyable. It's my first encounter with a framework that has binding capabilities, and I've been happily working on some personal projects with it.

However, I've hit a roadblock trying to integrate bootstrap5. While I've managed to successfully import both the style sheets and JavaScript from the CDN, as well as locally within app.html, I'm now facing a challenge in opening a modal without requiring a button press (though not essential for my current project, I believe it may become necessary in the future). I've tried referencing bootstrap.Modal from the page.svelte file.

Here are the issues I encountered in attempting to achieve this:

  1. When I try importing the library at the top of the script within +page.svelte using 'import bootstrap from "$lib/bs5/bootstrap.bundle.min.js"', I receive an error stating "document is not defined." I assume this is because the JS file is trying to reference document, which must be done using Svelte's onmount. However, I'm unable to figure out how to import within an onmount.

  2. If I remove the import statement and load the JS file locally, I can reference "bootstrap.Modal" as needed, but VS Code/TypeScript doesn't support intellisense and consistently flags an error due to the lack of a direct reference to bootstrap.Modal. While this workaround functions, I suspect there might be a better way to address this issue. Essentially, it works but lacks intellisense support and constantly shows errors.

I came across "sveltestrap," but it seems a bit overwhelming for my needs. I simply want to utilize the same features found in the official Bootstrap documentation, and sveltestrap appears to introduce many differences.

Has anyone encountered similar challenges before? Any insights on resolving this error would be greatly appreciated.

Answer №1

When trying to import the Bootstrap bundle in a Svelte component using an import statement, you may encounter difficulties. The best approach is to import it using a <script> tag within the app.html file, which serves as the root page. Placing it at the end of the <body> tag is recommended, although the exact placement is not crucial.

In terms of typing support, visiting the package page on npmjs.com reveals a "DT" icon indicating that there is a sibling package named @types/bootstrap that offers typing functionalities. Installing this package will enable Intellisense for your project.

https://i.sstatic.net/Z4zUCBTm.png

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Control the movement of an item within a threejs environment using your mouse

I am struggling to move an object within a threejs scene as I am unable to select the object when clicking with the mouse. I attempted to adapt the code provided in this link (https://github.com/mrdoob/three.js/blob/master/examples/webgl_interactive_dragga ...

Determine the quantity of elements within a JSON object based on specified criteria

I am seeking a way to count the number of items in an array of JSON objects that meet specific conditions. The structure of my array is as follows: array = [{ name: 'Bob', age: 24 }, ...., { ...

I am implementing vanilla JavaScript to send an AJAX post request to a Django server

I've been struggling with making an AJAX post request to Django using this JavaScript snippet: const xhr = new XMLHttpRequest(); console.log(xhr.readyState); xhr.open('POST', ''); var data = '{% csrf_token %}'; consol ...

Does an async function get automatically awaited if called in a constructor?

I am currently working on updating some code due to a library upgrade that requires it to be made async. The code in question has a base class that is inherited by other classes, and I need to call some functions in the constructor that are now asynchronou ...

Combine the results of callbacks executed in a loop and return the final concatenated value

I have a dilemma with my data stored in MongoDB. I am currently attempting to update the score whenever it is triggered. However, due to the nature of my task, I find myself needing to execute multiple queries depending on a loop. Ultimately, my goal is t ...

When working with Node.js and Express, I encountered an issue where the req.session object was not defined within

It's peculiar to me that req.session.username is undefined in the tag >>>DOESNT WORK<<< while it works in the tag >>>THIS DOES WORK<<<. I passed req as an argument to my module, but it seems like there might be some ...

Highcharts encounters issues with dateRange values disappearing after a refresh in older versions of IE (9 and below) and Chrome

const newCurrentIndex = findIndexForCounter(currentPCData.CounterID, currentPCData.NetworkID); if (currentIndex === newCurrentIndex) { $.each(model.Data, (j, point) => { ...

Memory leakage in Internet Explorer as a result of JavaScript code

Recently, I created a website that utilizes jquery ajax to send an ajax request every minute in order to retrieve json data. This data is then parsed and added into the DOM. While this process runs smoothly on Chrome and Firefox, I noticed a significant m ...

What could be causing the issue with retrieving HTTP requests in Nest.js even after configuring the controller?

After the individual who departed from the company utilized Nest.js to create this server-side system. They established the auth.controller, auth.service, auth.module, auth-token, jwt.strategy, and jwt-payload, with everything properly configured. I verifi ...

Loading fonts using next.js and style jsx

I've recently started the process of converting my create react app to next.js. As a reference, I've been using Vercel's open source Next.js website to help me structure my own. In order to implement custom fonts, I created a font.ts file an ...

Unable to execute any actions on object in JavaScript

I currently have two functions in my code: getRawData() and getBTRawData(). The purpose of getBTRawData() function is to retrieve data from Bluetooth connected to a mobile device. On the other hand, getRawData() function takes the return value from getB ...

Creating interactive tables in JavaScript with dynamic row adding, editing and deleting capabilities

Whenever I try to add a new row by clicking the Add Row button, an error occurs. My goal is to append a new 'tr' every time I click the add row button. Each 'td' should include a checkbox, first name, last name, email, mobile number, ed ...

Utilizing AngularJS to implement a Currency Filter on the output of a personalized filter

I'm currently working on a custom filter that transforms zero values into ' - ' for display purposes. The goal is to display currency formatting for non-zero values. However, I encountered an unexpected token error while trying to implement ...

Choose specific elements based on their attribute within a class, and then either apply or remove a class

I have a project where I need to store the data information of each element in my navigation menu when a button is clicked. This project is a one-page website with two levels of navigation: Main menu Button at the bottom of each "page" Currently, I hav ...

How to retrieve entire HTML page using an Ajax call in an ASP.NET web forms application

I'm currently working on an ASP.NET web forms application and running into an issue with making an AJAX call to a web method in the code-behind. Instead of getting the result I expect, it's returning the entire HTML page. The call is triggered b ...

I am looking to record the exact date and time when employees clock in and out, and store this information in a MySQL

To track employee clock-ins and clock-outs, I have created a MySQL table named emp_log. When an employee clicks a button, the current date and time are automatically inserted into the emp_log table, along with their user_id and clock_id. This process shou ...

Tips for setting up a Vue application (using vue-cli) to include nonce attributes in created script tags

My vue application, compiled using vue-cli, has a vue.config.js file structured as follows: 'use strict'; module.exports = { publicPath: `${process.env.CDN_URL || ''}/dist/`, lintOnSave: true, transpileDependencies: [], outputD ...

Axios appends square brackets at the end of the parameter name

Utilizing vuejs in combination with axios and a Django server presents a challenge. The server requires parameters to be passed as travelers, but when using axios to send this data, it appends [] at the end resulting in travelers[]. Is there a way to prev ...

The filter is displaying incorrect categories

I am facing an issue with creating a work filter based on the last column which represents categories. When I select an option from the dropdown, I want to display only that specific category and hide the others. Currently, when I try clicking on an option ...

Tips for using ng-repeat with a hardcoded value instead of an array

Is there a way to manually run the ng-repeat function a specific number of times without passing an array parameter? I attempted to hardcode the number in the ng-repeat attribute, but it didn't work as expected. <h1 ng-repeat="x in 20">{{sumofT ...