RequireJS 设置父级路径 [英] RequireJS Setting Path to the Parent Level

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

问题描述

为了将我的模型、集合等从我的移动应用程序定向到我的网络应用程序,我需要配置我的 RequireJS.

In order to direct my models, collections, etc. from my mobile app to my web app, I need to configure my RequireJS.

我有以下文件结构:

/
/js
  /models
/mobile
  /js
  /templates

我希望我的 mobile 应用程序使用我的主应用程序的模型,因此我设置了以下内容:

I want my mobile app to use my main app's models, so I set the following:

require.config( {
    paths : {
        models : '/js/models',
        templates : 'mobile/templates'
    }
});

我在 Chrome 中遇到这些错误:

And I get these errors in Chrome:

Uncaught SyntaxError: Unexpected token < /mobile/js/models/User.js?bust=0.1.0:1
Uncaught SyntaxError: Unexpected token < /mobile/js/models/InviteRequest.js?bust=0.1.0:1
Uncaught SyntaxError: Unexpected token < /mobile/js/models/Log.js?bust=0.1.0:1

意味着它仍然在/mobile而不是/中查找.

Meaning that it's still looking in /mobile instead of /.

此路径配置适用于我的 templates 文件夹,但不适用于我的 models 文件夹.如何将所有以 models/ 开头的 require 依赖项指向我的主模型文件夹?

This path config works for my templates folder, but not my models folder. How can I point all require dependencies that start with models/ to my main models folder?

谢谢!

尝试 2

我尝试改为设置 baseUrl:

require.config( {
    baseUrl : '/',
    paths : {
        views : 'mobile/views',
        templates : 'mobile/templates'
    }
});

require( [
    'app'
],
function(App) {
    App.initialize();
});

我收到错误:

GET http://local.m.mysite.co/app.js 404 (Not Found) require.js:2

推荐答案

在您的第二次尝试中,您基本上是告诉 Require JS 加载 'app' 模块,并且由于您将基本 url 设置为 '/',Require JS 将看起来 'app' 模块在您的根路径中.

In your second attempt, you basically tell Require JS to load 'app' module and since you set the base url to '/', Require JS will look 'app' module in your root path.

所以你需要做的是告诉 Require JS 在哪里寻找你的应用程序"模块.假设您的应用程序"模块在/js/module 中:

So what you need to do is to tell Require JS where to look for your 'app' module. Say your 'app' module is in /js/module:

/
/js
  /models
  /module
    /app.js
/mobile
  /js
  /templates

那么你加载app"模块的代码是:

Then your code to load 'app' module is:

require.config( {
    baseUrl : '/',
    paths : {
        views : 'mobile/views',
        templates : 'mobile/templates'
    }
});

require( [
  'js/module/app'
],
function(app) {
    app.initialize();
});

或者,您可以在配置中设置模块"路径.然后在你的依赖加载中引用这个路径:

Alternatively, you can set 'module' path in your config. Then refer to this path in your dependency loading:

require.config( {
    baseUrl : '/',
    paths : {
        views : 'mobile/views',
        templates : 'mobile/templates',
        module: 'js/module'
    }
});

require( [
  'module/app'
],
function(app) {
    app.initialize();
});

至于在 ATTEMPT 2 之前在 Chrome 中遇到的错误,您需要查看在模块加载中如何引用您的模块.

As for the error you get in Chrome before your ATTEMPT 2, you will need to look at how you refer to your module in module loading.

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

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