1

我正在尝试在我的 browserify 项目中加载 jquery-mobile 并收到错误,因为this引用的是空对象而不是window对象。Browserify 将 jquery-mobile 包装在它通常的包装器 ( function(require, module, exports){ ... }) 中,但this不在window.

包.json

{
  "dependencies": {
    "animate.css": "^3.1.1",
    "backbone": "^1.1.2",
    "backbone-query-parameters": "git://github.com/jhudson8/backbone-query-parameters",
    "backbone.marionette": "^2.3.1",
    "backbone.radio": "^0.6.0",
    "backbone.storage": "^0.1.0",
    "backbone.syphon": "^0.5.0",
    "bootstrap": "^3.3.1",
    "browserify-swap": "^0.2.1",
    "handlebars": "^1.3.0",
    "jquery": "^2.1.3",
    "jquery-mobile": "^1.4.1",
    "lodash": "^2.4.1",
    "nprogress": "^0.1.6"
  },
  "browser": {
    "bootstrap": "./node_modules/bootstrap/dist/js/bootstrap.js",
    "jquery-mobile": "./node_modules/jquery-mobile/dist/jquery.mobile.js"
  },
  "browserify-shim": {
    "bootstrap": {
      "depends": [
        "jquery:jQuery"
      ]
    },
    "jquery": "$",
    "jquery-mobile" : { "exports": "$.mobile", "depends": [ "jquery:$" ] }
  },
  "browserify-swap": {
    "@packages": [
      "underscore"
    ],
    "dist": {
      "underscore.js$": "lodash"
    }
  },
  "browserify": {
    "transform": [
      "babelify",
      [
        "hbsfy",
        {
          "extensions": [
            "hbs"
          ]
        }
      ],
      "browserify-swap",
      "browserify-shim"
    ]
  }
}

错误信息

jquery.mobile.js:14931未捕获类型错误:无法读取未定义的属性“jQuery”

然后我找到了moduleify

经过一番谷歌搜索后,我找到了moduleify,并尝试在我的package.json中为 jquery-mobile 使用它,但出现错误。这是我使用 moduleify 更新的转换配置:

  "browserify": {
    "transform": [
      "babelify",
      [
        "hbsfy",
        {
          "extensions": [
            "hbs"
          ]
        }
      ],
      [
        "moduleify",
        [
          "jquery.mobile"
        ]
      ],
      "browserify-swap",
      "browserify-shim"
    ]
  }

这样做会给我一个 moduleify 错误:

moduleify/index.js:9 var rules = Array.isArray(aliases) ? 别名:Object.keys(aliases).map(fu

有人对如何this参考有任何建议window吗?我觉得我在正确的轨道上,但不知道下一步该做什么。谢谢!

4

1 回答 1

0

在第一种方法中,您可以:

"jquery-mobile" : { "exports": "$.mobile", "depends": [ "jquery:$" ] }

我认为你需要这样做:

"jquery-mobile" : { "exports": "$.mobile", "depends": [ "jquery:jQuery" ] }
于 2015-03-11T04:09:50.307 回答