Import functions from another js file. Javascript

前端 未结 5 942
臣服心动
臣服心动 2020-12-02 23:02

I have a question about including a file in javascript. I have a very simple example:

--> index.html
--> models
      --> course.js
      --> s         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 23:35

    By default, scripts can't handle imports like that directly. You're probably getting another error about not being able to get Course or not doing the import.

    If you add type="module" to your


    If you're serving files over file://, it likely won't work. Some IDEs have a way to run a quick sever.

    You can also write a quick express server to serve your files (install Node if you don't have it):

    //package.json
    {
      "scripts": { "start": "node server" },
      "dependencies": { "express": "latest" }
    }
    
    // server/index.js
    const express = require('express');
    const app = express();
    
    app.use('/', express.static('PATH_TO_YOUR_FILES_HERE');
    app.listen(8000);
    

    With those two files, run npm install, then npm start and you'll have a server running over http://localhost:8000 which should point to your files.

提交回复
热议问题