Does Angular need a web server to run? So would Angular work if I give browser a url of a local file?

前端 未结 5 1258
无人共我
无人共我 2021-02-12 10:55

By this I mean I have read that Angular allows mock up data to be used so that RESTFul apis need not be wired up. I can think of a use case where a UX designer need only look a

5条回答
  •  孤独总比滥情好
    2021-02-12 11:09

    As others have said, it's best to serve properly as http. However, there are other workarounds.

    Some editors, like Brackets (click on the lightning bolt in the top right corner while in a file), can serve the code to your browser properly. For others there might be plugins that do it.

    Update: My suggestion below worked well enough for AngularJS 1, but just FYI is insufficient for Angular 2. Also see Disable same origin policy in Chrome

    Further, if you're on Chrome you can run it with flags, which means you add some stuff at behind the .exe part of the path on a short cut; options if you will. Specifically you'd want:

    --allow-file-access-from-files --allow-file-access --allow-cross-origin-auth-prompt
    

    That makes it not throw errors when trying to access files from various origins. There was a plugin for that once, but I couldn't get it to work. Note there's security reaosns why this isn't the default, so maybe don't put it on your main short cut that you use all the time for surfing... - Use at own risk.

提交回复
热议问题