相对路径不适用于路径 [英] relative path doesn't work with paths

查看:112
本文介绍了相对路径不适用于路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在requirejs中的相对路径有问题.

I'm having a problem with relative paths in requirejs.

首先,我具有以下结构.我正在使用虚拟主机(os.com)运行它,路径是os.com/test

First of all, I have the following structure. I'm running it with a virtual host (os.com) and the path is os.com/test

index.html

<script data-main="config" src="require.js"></script>

config.js

require.config({

  baseUrl: "./apps",
  deps: ['ui'],
  paths: {
    ui: 'ui/ui',
    system: 'system/system',
    core: 'core/core'
  }


});

ui.js

define(['./class/menuBuilder',"./class/window"], function(menuBuilder, windowBuilder){


    return {
        menuBuilder: menuBuilder,
        windowBuilder: windowBuilder
    }

});

运行它时,出现以下错误.

When I run it, I get the following errors.

GET http://os.com/test/apps/class/menuBuilder.js 404 (Not Found)
GET http://os.com/test/apps/class/window.js 404 (Not Found)

如果我从'paths'属性中取出'ui'属性,然后将deps更改为['ui/ui'],则可以,但是我想使用路径.

If I take out 'ui' property from the 'paths' property then change deps to ['ui/ui'], it works, but I would like to use paths.

更改了config.js

require.config({

  baseUrl: "./apps",
  deps: ['ui/ui'],
  paths: {
    system: 'system/system',
    core: 'core/core'
  }


});

如何更改配置以使路径和相对路径协同工作?

How do I change my config to make paths and relative path work together?

推荐答案

我遇到了类似的问题.我在配置中没有deps:'ui'部分,只有路径设置,但是相对模块引用('./class/menuBuilder')仍然无法从加载了路径的模块("ui:'ui /ui'),并改用baseUrl.为了解决这个问题,我将"ui"定义为一个包:

I had similar problem. I didn't have deps:'ui' part in my configuration, just the path setting, but still the relative module reference ('./class/menuBuilder') would not work from the module loaded with path ("ui: 'ui/ui'") and would use baseUrl instead. To solve it, I defined 'ui' as a package:

require.config({

  baseUrl: "./apps",
  deps: ['ui'],
  paths: {
    system: 'system/system',
    core: 'core/core'
  },
  packages : [
    { 
        name: 'ui',
        location : 'ui',
        main : 'ui'
    },
  ]

});

在这种情况下,require将正确加载相对路径的模块.

In this case require will load relatively-pathed modules correctly.

这是一篇有用的文章:带有RequireJS模块/软件包的相对路径

Here is a useful post: Relative paths with RequireJS modules/packages

这篇关于相对路径不适用于路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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