You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Aurelia框架中使用Bootstrap-tooltip报错问题求助

Fixing "Bootstrap_1.default is not a function" in Aurelia Bootstrap Tooltip

Hey there, let's break down why you're seeing that error and get your Bootstrap Tooltip working smoothly in Aurelia.

The Root Cause

That error pops up because your Bootstrap import approach doesn't match the version you're using:

  • Bootstrap 5+: It dropped jQuery dependency entirely, and no longer exports a global $ function by default. Your current import $ from "bootstrap"; pulls in an object of Bootstrap components, not a callable function like jQuery.
  • Bootstrap 4: It still relies on jQuery, but you need to ensure jQuery loads first so Bootstrap can attach its plugin methods to jQuery's prototype.

Solution 1: Bootstrap 5+ (Modern, jQuery-Free)

Bootstrap 5 exposes individual component classes that you instantiate directly. Update your custom attribute like this:

import { customAttribute, inject } from "aurelia-framework";
import { Tooltip } from "bootstrap";

@customAttribute("bootstrap-tooltip")
@inject(Element)
export class BootstrapTooltip {
  constructor(element) {
    this.element = element;
    this.tooltipInstance = null;
  }

  bind() {
    // Create a Tooltip instance for the target element
    this.tooltipInstance = new Tooltip(this.element);
  }

  unbind() {
    // Clean up the instance to avoid memory leaks
    if (this.tooltipInstance) {
      this.tooltipInstance.dispose();
    }
  }
}

Key changes:

  • We import the Tooltip class directly from Bootstrap instead of trying to use a $ wrapper.
  • We store the tooltip instance to properly dispose of it later (using Bootstrap 5's dispose() method instead of the old destroy()).

Solution 2: Bootstrap 4 (jQuery-Dependent)

If you're using Bootstrap 4, you need to import jQuery first, then Bootstrap (so it can attach its plugins to jQuery):

import { customAttribute, inject } from "aurelia-framework";
import $ from "jquery";
// Import Bootstrap to attach its methods to jQuery's prototype
import "bootstrap";

@customAttribute("bootstrap-tooltip")
@inject(Element)
export class BootstrapTooltip {
  constructor(element) { this.element = element; }

  bind() { $(this.element).tooltip(); }

  unbind() { $(this.element).tooltip("destroy"); }
}

Make sure you've installed both dependencies:

npm install jquery bootstrap@4

Quick Extra Step

Don't forget to include Bootstrap's CSS in your Aurelia app (usually in your main.js or root component):

import "bootstrap/dist/css/bootstrap.min.css";

This ensures your tooltip has proper styling instead of appearing as plain, unformatted text.

内容的提问来源于stack exchange,提问作者vhurryharry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:14:27