在requirejs参考束 [英] Reference bundles on requirejs

查看:137
本文介绍了在requirejs参考束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个已经使用了捆绑和ASP.Net MVC的微小旧的Web应用程序。

I have an old web application that already uses bundling and minification of ASP.Net MVC.

现在我开始使用requirejs,我有一个问题!如何从requirejs引用包文件?

Now I'm starting to use requirejs and I have a problem! How can I reference bundle files from requirejs?

我知道还有另一个类似的问题,但它并没有回答我的问题!

I know that there is another similar question, but it does not answer to my question!

<一个href=\"http://stackoverflow.com/questions/12414654/how-to-reference-the-bundled-js-files-asp-net-mvc4-in-require-js/12428681\">How在Require.js引用捆绑的js文件(asp.net mvc4)?

推荐答案

我希望我正确地理解你的问题。

I'm hoping that I understand your question correctly.

如果您的软件包中包含 requireJS模块和你加载它的网页上,你仍然可以调用这些模块只要你定义他们像我有如下:

If your bundle contains requireJS modules and you're loading it on the page, you can still call those modules as long as you've defined them like I have below:

假设你现有的包正在呈现这样的:

Let's say your existing bundle is being rendered like this:

@Scripts.Render("~/bundles/aCustomBundle")

该软件包包含的jQuery 阵营 aLibrary 模块。在 aLibrary 被定义为在该包遵从。

The bundle contains jQuery, React and a aLibrary module. The aLibrary is defined as follows in that bundle.

// Define custom modules
define('aLibrary', [], function() {
   return {
      aFunction: function() {
           console.log('a function within a library');
      }
   }
});

在你的页面,你可以调用模块和这样定义的第三方库。

In your page, you can call that module and define the third-party libraries like this.

<script>
    // Third party libraries
    define('jquery', [], function () { return jQuery; });
    define('react', [], function () { return React; });

    // Use the libs from the bundle
    require(['aLibrary', 'jquery', 'react'], function(a, j, r) {

       // Execute the function from the aLibrary
       a.aFunction();
    });
</script>

这篇关于在requirejs参考束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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