Requirejs依赖性不工作

编程入门 行业动态 更新时间:2024-10-12 05:54:50
本文介绍了Requirejs依赖性不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图使用角ladda 指令在我的应用程序。这里是我的main.js这是requirejs宣言文件:

I am trying to use angular-ladda directive in my app. Here is my main.js which is the requirejs manifesto file:

requirejs.config({ paths: { underscore: 'lib/underscore/underscore', angular: 'lib/angular/angular', jquery: 'lib/jquery/dist/jquery.min', 'angular-route': 'lib/angular-route/angular-route', 'controllers': 'js/controllers', 'services': 'js/services', 'providers': 'js/providers', 'filters': 'js/filters', 'directives': 'js/directives', 'app': 'js/app', 'spin': 'lib/ladda/dist/spin.min', 'ladda': 'lib/ladda/js/ladda', 'ngLadda': 'lib/angular-ladda/src/angular-ladda' }, shim: { underscore: { exports: '_' }, 'angular': { exports: 'angular' }, 'states': { deps: ['angular'], exports: 'states' }, 'angular-route': { deps: ['angular'] }, 'ladda': { deps: ['spin'], exports: 'Ladda' }, 'ngLadda': { deps: ['ladda'] } }, priority: [ 'angular' ] }); requirejs(['angular', 'app', 'underscore', 'js/routes', 'jquery', 'services/services', 'providers/providers', 'directives/directives', 'filters/filters', 'controllers/controllers', 'ngLadda' ], function (angular, app, _) { angular.element(document).ready(function () { angular.bootstrap(document, ['App']); document.getElementsByTagName('html')[0].dataset.ngApp = 'App'; }); });

下面是ladda指令:

Here is the ladda directive:

/**! * AngularJS Ladda directive * @author Chungsub Kim <subicura@subicura> */ /* global Ladda */ (function () { 'use strict'; angular.module('angular-ladda', []).directive( 'ladda', [ '$compile', function ($compile) { return { restrict: 'A', link: function (scope, element, attrs) { element.addClass('ladda-button'); if(angular.isUndefined(element.attr('data-style'))) { element.attr('data-style', 'zoom-in'); } var ladda = Ladda.create( element[0] ); //here is were Ladda is not defined $compile(angular.element(element.children()[0]).contents())(scope); scope.$watch(attrs.ladda, function(loading) { if(loading || angular.isNumber(loading)) { if(!ladda.isLoading()) { ladda.start(); } if(angular.isNumber(loading)) { ladda.setProgress(loading); } } else { ladda.stop(); } }); } }; } ] ); })();

在指令code(22行,其中注释)有Ladda没有定义的erroe。结果如果我将添加以下行:

In the directive code (line 22, where commented) there is an erroe "Ladda is not defined". If I will add the following line:

var Ladda = require('ladda');

这一切都将正常工作,所以为什么在main.js的依赖性​​不这样做了吗?

It all will work, so why the dependency in the main.js not doing this already?

您可以请帮我这个吗?

推荐答案

我觉得对于依赖工作,你必须定义ngLadda声明作为一个模块

I think for the dependency to work, you'd have to define ngLadda declaration as a module

define(function (require) { // your ngLadda declaration here }

编辑:还有另外一种可能当你看Ladda的code: github/hakimel/Ladda/blob/master/js /ladda.js

您看到Ladda被包裹在定义(){}

You see that Ladda is being wrapped in define(){}

// AMD module else if( typeof define === 'function' && define.amd ) { define( [ 'spin' ], factory ); }

在requireJS文档它指出:

In requireJS docs it's stated that:

只能使用其他的垫片模块,依赖关系匀脚本,或有不依赖AMD库和调用定义()后,他们也创造了一个全球性的(像jQuery或lodash)。否则,如果您使用的是AMD模块作为一个垫片配置模块的依赖关系,构建后,即AMD模块可直到在构建被匀code后评估执行,并会发生错误。最终的解决办法是升级所有匀code有可选的AMD定义()调用。

Only use other "shim" modules as dependencies for shimmed scripts, or AMD libraries that have no dependencies and call define() after they also create a global (like jQuery or lodash). Otherwise, if you use an AMD module as a dependency for a shim config module, after a build, that AMD module may not be evaluated until after the shimmed code in the build executes, and an error will occur. The ultimate fix is to upgrade all the shimmed code to have optional AMD define() calls.

requirejs/docs/api.html#config-shim

所以,Ladda是AMD模块DEPS已定义。在你main.js把它垫片之外:

So, Ladda is an AMD module with deps already defined. In your main.js put it outside of shim:

'ladda': { exports: 'Ladda' }, 'ngLadda': { deps: ['ladda'] } shim: { //your other shimmed modules }

把你ngLadda的定义(功能(需要){})像它在原来的答复的说,但不要把任何依赖内。

Put your ngLadda in define(function(require){}) like it's said in original answer, but don't put any dependencies inside.

更多推荐

Requirejs依赖性不工作

本文发布于:2023-11-30 22:18:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651638.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:依赖性   工作   Requirejs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!