Domanda

Sto usando un progetto di esempio da qui.

Supponiamo di dover esportare alcune funzione dal mio modulo per fornire un'API JavaScript ai clienti del mio servizio.

Ma le dichiarazioni nei miei file .js non sono visibili al di fuori di Requisiti!

Aggiungo il seguente blocco a JQuery-richiedere Sample/WebApp/App.html:

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

Fallisce: TypeEerror non integrato: Object [Object Object] non ha un metodo 'alfa'.

È possibile fare quello che voglio?

È stato utile?

Soluzione

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>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top