如何修复此AMD路径冲突? [英] How can I fix this AMD path conflict?

查看:117
本文介绍了如何修复此AMD路径冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Esri ArgGis JavaScript API ,这是由 Dojo 加载,使用 dojo.require 。我有一个现有的模块化 AMD / requirejs Typescript应用程序,我需要将此代码集成到其中。在我的初始TS文件的顶部,我导入了几个模块:

I am trying to use the Esri ArgGis JavaScript API, which is loaded by Dojo, using dojo.require. I have an existing modular AMD/requirejs Typescript application that I need to integrate this code into. At the top of my initial TS file, I import several modules:

import tracer = module('../classes/trace');
import pubsub = module('../classes/pubsub');
import masker = module('../classes/masker');
// etc.

这个工作正常,但现在我添加了ArcGis代码,而不是解析我的应用程序中的相对路径,require.js从Esri站点获取了一个baseUrl,并尝试加载:

This was working fine, but now that I have added the ArcGis code, instead of resolving the relative path within my application, require.js has picked up a baseUrl from the Esri site, and tries to load:

http://serverapi.arcgisonline.com/jsapi/arcgis/3.3/js/esri/classes/trace.js
// etc.

导致404响应和脚本错误的字符串。

Resulting in a string of 404 responses and script errors.

我该如何解决这个问题?

How can I fix this?

在加载第一个加载模块的文档之前,我已经尝试在我的html文件的头部设置requirejs baseUrl:

I've tried setting the requirejs baseUrl in the head of my html file before loading the first document that loads modules:

 <script src="http://serverapi.arcgisonline.com/jsapi/arcgis/3.3"></script>
 <script type="text/javascript" src="/content/client/libs/require.js"></script> <!-- data-main="/content/client/hop/hop.app" -->
 <script type="text/ecmascript">
        require.config({
            baseUrl: "/Content/client/hop/"
        });
 </script>
 <script src="~/Content/client/hop/hop.app.js"></script>

但这会失败,抛出一个需要没有方法配置的异常。

But this fails, throwing an exception that require has no method config.

(注意如果我颠倒了html文档头部的顺序,以便arcgis api在加载序列中排在最后,那么我就会遇到相反的问题 - 我的本地文件都工作正常但是dojo和映射api失败,因为他们正在寻找相对于我的站点的路径,当他们应该在argis服务器上搜索时)。

(NB If I reverse the order in the head of the html document so that the arcgis api comes last in the load sequence then I get the opposite problem - my local files all work fine but dojo and the mapping api fail because they are looking for paths relative to my site when they should be searching on the argis server).

推荐答案

我使用Esri的ArcGIS API,所以我遇到了这个问题。 来自dojo的这篇博客文章帮助了我一些。

I work with Esri's ArcGIS API so I've run into this exact problem. This blog post from dojo helped me out some.

第一个问题是dojo的配置方式与requirejs不同。它寻找一个先前定义的dojoConfig进行设置。第二个是Esri的模块加载是在假定一个基本路径的情况下设置的,而你的代码将需要另一个。你将需要一个看起来像这样的dojo配置:

The first issue is that dojo isn't configured the same way requirejs is. It looks for a previously defined dojoConfig to set things up. The second is that Esri's module loading is all set up assuming one basepath, and your code is going to want another. You're going to need a dojo config that looks something like this:

dojoConfig = {
    baseUrl: location.pathname.replace(/\/[^/]+$/, '') + '/Content/client/hop/',  // magic!
    packages: [
        {
            name: 'dojo',
            location: "http://serverapi.arcgisonline.com/jsapi/arcgis/3.3compact/js/dojo/dojo/"
        },
        {
            name: 'dojox',
            location: "http://serverapi.arcgisonline.com/jsapi/arcgis/3.3compact/js/dojo/dojox"
        },
        {
            name: 'esri',
            location: "http://serverapi.arcgisonline.com/jsapi/arcgis/3.3compact/js/esri"
        }
    ]
};

这样做是将basepath设置回当前url加上你的额外内容,然后告诉esjo的东西是道场。这些都是我遇到的所有软件包但是如果有一个依赖项我错过了,因为它从来没有为我加载过,它需要一个类似的条目。

What this is doing is setting the basepath back to the current url plus your extra stuff, and then telling dojo where esri's stuff is. Those are all the packages that I've run into but if there's a dependency I missed because it just never loaded for me, it will need a similar entry.

另一个问题您可能遇到的问题是,如果您习惯在本地将脚本作为文件加载://现在来自另一个域的dojo将尝试访问file://并且浏览器将关闭该文件。从现在开始,您需要在本地http服务器上进行测试。在Windows上我更喜欢 HFS 和Linux python让它变得简单

Another problem you might run into is that if you're used to loading your script locally as file:// now dojo from another domain is going to try to access file:// and the browser is going to shut that right down. You'll need to test on a local http server from now on. On Windows I prefer HFS and on Linux python makes it easy.

我希望这会有所帮助。

这篇关于如何修复此AMD路径冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆