backbone.js Browserify包未定义依赖关系

uurity8g  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(154)

我在一个Backbone项目中使用browserify,并填充owlCarousel,因为它不是一个类似commonJS的模块。shim转换似乎可以工作,但在我真正需要它的时候却不行。
我的app.js文件看起来像:

var settingsView = require("./settingsView.js");

new settingsView();

和我的settingsView

var app = require("./controllers.js"),
    $ = require("jQuery"),
    Backbone = require("Backbone"),
    owlCarousel = require("owlCarousel");
    Backbone.$ = $;

module.exports = Backbone.View.extend({
   el: ".settings",

   events: {
    "click .carousel-img": "setBackground",
    "click .palette div": "setTextColor"
   },

   options: {
    singleItem: true,
    pagination: false,
    navigation: true,
    mouseDrag: false
   },

   initialize: function () {
     this.initCarousel();
   },

   initCarousel: function () {
     /* This is causing the problem */
      this.owlCarousel = this.$el.find(".carousel").owlCarousel(this.options);
   }
})

我的package.json

"browser": {
    "jquery": "./node_modules/jquery/dist/jquery.js",
    "owlCarousel": "./vendor/owl-carousel/owl-carousel/owl.carousel.js"
   },
   "browserify": {
    "transform": [
      "browserify-shim"
    ]
   },
   "browserify-shim": {
    "jquery": "$",
    "owlCarousel": {
      "depends": ["jquery:$"]
    }
   }

它给我的错误是:
未捕获的类型错误:这个.$el.find(...).owlCarousel不是函数
但是,如果我在dom就绪后在控制台中运行$("div").owlCarousel,它就可以工作了。

ma8fv8wu

ma8fv8wu1#

我解决了做

var app = require("./controllers.js"),
    $ = require("jQuery"),
    Backbone = require("Backbone"),
    owlCarousel = require("owlCarousel");
    Backbone.$ = $;    
    $.fn.owlCarousel = owlCarousel

相关问题