문제

I'm using a sample project from here.

Suppose I need to export some function from my module to provide some JavaScript API to the clients of my service.

But the declarations in my .js files are not visible outside RequireJS!

I add the following block to jquery-require-sample/webapp/app.html:

<script type="text/javascript">
   $(document).ready(function() {
      $('body').alpha().beta();
   });
</script>

It fails: Uncaught TypeError: Object [object Object] has no method 'alpha'.

Is it possible to do what I want?

도움이 되었습니까?

해결책

Based on the code you provided I'm assuming you added your code after the existing script tag in app.html. I think what you're seeing is a timing issue. After you load the page, take a look at the <head> tag and you should see script tags in the following order:

  1. the "require" script
  2. your new script
  3. alpha
  4. beta

so it's running your script before the alpha and beta are run. The reason is because require will process the first script, but not execute the "meat" of main.js until all it's dependencies are run (alpha and beta).

I hope this helps. The following changes to your code may also illustrate what's going on. the setTimeout gives alpha and beta a chance to load:

<script type="text/javascript">
  setTimeout(function(){
           $(document).ready(function() {
              $('body').alpha().beta();
           });
           }, 5000);
</script>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top