【问题标题】:Using mutiple versions of jquery in a SPA using durandal使用 durandal 在 SPA 中使用多个版本的 jquery
【发布时间】:2015-04-02 08:38:09
【问题描述】:

我正在使用 durandal 和 knout 和 require 进行 SPA。但是,我有一个依赖于我阅读的 jquery-latest.js 的脚本,不应再使用它。然而,jquery-1.4.2.js 似乎对我的脚本在 SPA 环境之外的测试工作正常,但我也使用 1.9.1 来处理我的 SPA 中的大部分内容。有没有办法同时使用这两个版本?我将如何在 main.js 中引用它们:目前我一直在做:

    'text'              : '../scripts/text',
    'durandal'          : '../scripts/durandal',
    'plugins'           : '../scripts/durandal/plugins',
    'transitions'       : '../scripts/durandal/transitions',
    'ko'                : '../scripts/knockout-3.1.0.debug',
    'jquery-latest'     : '../scripts/jquery-latest',//try-not allowed/want 1.4.2 instead
    'jquery'            : '../scripts/jquery-1.9.1',

任何帮助将不胜感激。

【问题讨论】:

  • jquery-latest 只是 jquery 的最新版本。你确定它不适用于 1.9.1 吗?我认为 durandal 应该与最新的 jquery 一起使用。只需将 jquery-1.9.1 升级到 jquery-1.11.2
  • @Wayne Ellery 感谢您的回复。我已经尝试了您的建议,但仍然不正确。有没有办法同时引用 1.9.1 和 1.4.2?
  • 为什么上面的尝试不起作用?我原以为它会奏效。
  • IIRC jQuery 设置一个全局变量 $ 或 jQuery。 jQuery 的两个版本都不会尝试使用它吗?我想最后一个注册的人会赢。我认为您需要将其中一个重新定义为其他内容。或更新您的脚本以与单个版本兼容。
  • 感谢您的回复,但我最终“重新创建”了我的应用程序并使用了 jquery-1.11.2。这是一项任务,但似乎是处理我的情况的更清洁的方式

标签: jquery knockout.js requirejs single-page-application durandal


【解决方案1】:

试试这个。这将包括第一个页面加载的所有依赖项,您将能够使用它们中的每一个。并将其命名为 BundleConfig.cs

using System;
using System.Web.Optimization;

namespace NameXyz
{
public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.IgnoreList.Clear();
        AddDefaultIgnorePatterns(bundles.IgnoreList);

        bundles.Add(
          new ScriptBundle("~/scripts/vendor")
            .Include("~/scripts/jquery-{version}.js")
            .Include("~/scripts/knockout-{version}.debug.js")
            .Include("~/scripts/toastr.js")
            .Include("~/scripts/Q.js")
            .Include("~/scripts/breeze.debug.js")
            .Include("~/scripts/bootstrap.js")
            .Include("~/scripts/moment.js")
          );

        bundles.Add(
          new StyleBundle("~/Content/css")
            .Include("~/Content/ie10mobile.css")
            .Include("~/Content/bootstrap.css")
            .Include("~/Content/bootstrap-responsive.css")
            .Include("~/Content/durandal.css")
            .Include("~/Content/toastr.css")
            .Include("~/Content/app.css")
          );
    }

    public static void AddDefaultIgnorePatterns(IgnoreList ignoreList)
    {
        if (ignoreList == null)
        {
            throw new ArgumentNullException("ignoreList");
        }

        ignoreList.Ignore("*.intellisense.js");
        ignoreList.Ignore("*-vsdoc.js");

        //ignoreList.Ignore("*.debug.js", OptimizationMode.WhenEnabled);
        //ignoreList.Ignore("*.min.js", OptimizationMode.WhenDisabled);
        //ignoreList.Ignore("*.min.css", OptimizationMode.WhenDisabled);
    }
}

}

现在转到 Global.asax 如果没有创建它

using System.Web;
using System.Web.Http;
using System.Web.Mvc;

using System.Web.Optimization;
using System.Web.Routing;

namespace NameXyz
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}

【讨论】:

    猜你喜欢
    • 2013-06-03
    • 2013-02-15
    • 2017-03-08
    • 2013-03-07
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多