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

使用RequireJS集成svgPanZoom库时出现Uncaught TypeError: svgPanZoom is not a function错误求助

Fixing "Uncaught TypeError: svgPanZoom is not a function" with RequireJS and svg-pan-zoom

I’ve run into this exact issue before when integrating non-AMD libraries with RequireJS—let’s break down what’s going wrong and how to fix it:

The Root Issue

Your code assumes svgPanZoom is being passed directly to your RequireJS callback as a function, but the library likely isn’t packaged to support AMD modules natively. This means RequireJS can’t automatically map it to the callback parameter, leaving svgPanZoom as an undefined or non-function value.

Solutions to Try

1. Use RequireJS Shim Configuration (Most Common Fix)

If svg-pan-zoom exposes itself as a global variable (like window.svgPanZoom), you need to tell RequireJS explicitly to map that global to your module import.

First, add this configuration to your main RequireJS entry file:

require.config({
  paths: {
    // Create a friendly alias for the library path
    'svg-pan-zoom': '/file/WebWidgets/js/libraries/svg-pan-zoom'
  },
  shim: {
    // Tell RequireJS this library exports a global named "svgPanZoom"
    'svg-pan-zoom': {
      exports: 'svgPanZoom'
    }
  }
});

Then update your util.js to use the mapped alias instead of the absolute URL:

define([ 'baja!', 'jquery', 'svg-pan-zoom' ], function ( baja, $, svgPanZoom) {
    'use strict';
    const updateInitializeDiv = () => {
        const svgDocument = $('#svgObjectElementFromBinding')[0].contentDocument;
        const svgDocumentElement = svgDocument.documentElement;
        console.log(svgDocumentElement);
        console.log(svgDocumentElement.tagName);//svg
        let panZoomSVG = svgPanZoom(svgDocumentElement, {
            zoomEnabled: true,
            controlIconsEnabled: true
        });
    }
    const util = {};
    util.updateInitializeDiv = updateInitializeDiv;
    return util;
});

2. Check for UMD-Wrapped Exports

Some modern libraries use UMD (Universal Module Definition), which might tuck the actual function under a default property. If the shim approach doesn’t work, modify your function call like this:

// Replace this line:
let panZoomSVG = svgPanZoom(svgDocumentElement, { ... });

// With this:
let panZoomSVG = svgPanZoom.default(svgDocumentElement, {
    zoomEnabled: true,
    controlIconsEnabled: true
});

Quick Verification Steps

  • Open your browser’s dev console and type svgPanZoom—if it shows up as a global function, the shim method will work.
  • Log the svgPanZoom parameter in your callback to inspect its structure; if it’s an object instead of a function, look for a default or similar property holding the actual pan/zoom function.

内容的提问来源于stack exchange,提问作者P.Ellis

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 16:12:29