Aquí está mi archivo de índice
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta content="IE=Edge" http-equiv="X-UA-Compatible"> <meta name="description" content="test"> <!-- iOS meta tags & icons --> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <meta name="apple-mobile-web-app-title" content="test"> <title>test</title> </head> <body> <script type="text/javascript"> console.log('myname'); calcbymodel('/model.json',[0,0,0,0]); </script> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/fetch-polyfill@0.8.2/fetch.js"></script><!-- fetch --> <script src="./babel.min.js"></script><!-- babel --> <script src="./app.js" type='text/babel'></script> </body> </html>Y aquí está el archivo app.js
async function calcbymodel(path, input) { const model = await tf.loadLayersModel(path); const r = model.predict(tf.tensor(input)); r.data().then(array => console.log(array)); return r.data(); }Me pregunto por qué la consola dice:
Uncaught ReferenceError: calcbymodel is not defined at (index):21 (anonymous) @ (index):21Para el script de babel, no se pudo importar como lo niega la política de la empresa. Entonces, lo importé como un archivo local. Aquí está el enlace:
https://unpkg.com/@babel/standalone/babel.min.js¿Alguien puede ayudarme?
Resulta que el problema del guión de babel. Aquí está el enlace: ¿Por qué JS no puede encontrar la función en el script de babel?
Debe mover el archivo app.js arriba donde está usando la función. En este caso, debe mover la etiqueta del script que está usando la función debajo del script app.js, o mover el script app.js arriba de donde usa la función.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta content="IE=Edge" http-equiv="X-UA-Compatible"> <meta name="description" content="test"> <!-- iOS meta tags & icons --> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <meta name="apple-mobile-web-app-title" content="test"> <title>test</title> </head> <body> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"> </script> <script src="https://cdn.jsdelivr.net/npm/fetch-polyfill@0.8.2/fetch.js"> </script><!-- fetch --> <script src="./babel.min.js"></script><!-- babel --> <script src="./app.js" type='text/babel'></script> <script type="text/javascript"> console.log('myname'); calcbymodel('/model.json',[0,0,0,0]); </script> </body> </html>Debido a que HTML carga archivos js en el orden en que están en el archivo, se puede usar una función en cualquier etiqueta de secuencia de comandos debajo de ella en el archivo HTML.
El JS se ejecuta actualmente en el orden que lo ha definido:
<body> <body> <!-- This is first --> <script type="text/javascript"> console.log('myname'); calcbymodel('/model.json', [0, 0, 0, 0]); </script> <!-- This is second--> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"></script> <!-- This is third--> <script src="https://cdn.jsdelivr.net/npm/fetch-polyfill@0.8.2/fetch.js"></script> <!-- fetch --> <!-- This is fourth--> <script src="./babel.min.js"></script> <!-- babel --> <!-- This is fitht--> <script src="./app.js" type="text/babel"></script> </body> Si desea acceder a su código desde otra función, debe mover su primer script al final, el segundo problema parece ser que type="text/babel" debe configurarse en el script babel y no en la aplicación.
Lo probé con CDN y esta aplicación:
<body> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/fetch-polyfill@0.8.2/fetch.js"></script> <script src="./app.js" ></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js" type="text/babel"></script> <script type="text/javascript"> console.log('myname'); calcbymodel('/model.json', [0, 0, 0, 0]); </script> </body>en aplicación.js:
const calcbymodel = ()=>{ console.log("make calculations"); }