解决require()未声明问题(require.js实例,小白)
目录结构:
代码:
Test_graph.html:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 </head> 7 <body> 8 <script src='../../js/jquery-3.3.1.min.js'></script> 9 <script src="./REQUIRE/lib/require.js" data-main="./REQUIRE/app"></script> 10 <script src='REQUIRE/app/ngraph.graph.min.js'></script> 11 </body> 12 </html>
REQUIRE/app.js
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 // For any third party dependencies, like jQuery, place them in the lib folder. 2 3 // Configure loading modules from the lib directory, 4 // except for 'app' ones, which are in a sibling 5 // directory. 6 requirejs.config({ 7 baseUrl: './REQUIRE/lib', 8 paths: { 9 app: '../app' 10 } 11 }); 12 13 // Start loading the main app file. Put all of 14 // your application logic in there. 15 requirejs(['./app/main']);
REQUIRE/lib/require.js:https://requirejs.org/docs/release/2.3.6/comments/require.js
REQUIRE/app/ngraph.graph.min.js:https://unpkg.com/ngraph.graph@19.0.0/dist/ngraph.graph.min.js
REQUIRE/app/main.js
1 define(function (require) { 2 // Load any app-specific modules 3 // with a relative require call, 4 // like: 5 alert("**1*"); 6 var createGraph = require('./ngraph.graph.min'); 7 alert("**2*"); 8 var g = createGraph(); 9 g.addNode('hello'); 10 g.addNode('world', 'custom data'); 11 g.addLink('hello', 'world'); 12 g.addNode('server', { 13 status: 'on', 14 ip: '127.0.0.1' 15 }); 16 var world=g.getNode("world"); 17 alert(JSON.stringify(world)); 18 19 });