Routes in DART, s server required?

2019-08-31 23:14发布

问题:

I tried both AngularDART routes, and routes with Polymer, this example

but found, that none is working once I open the file from Dartium directly, but both work perfectly once I run them from Dart editor!

am I doing something wrong!

thanks

回答1:

This is because when you run from DartEditor (Ctrl+R) pub serve is launched which acts as a web server for the browser. This doesn't mean that you need a web server to run this example though.
What makes the difference is that pub serve runs transformers that do some sorts of code modification on the fly when the browser requests a page from the server.

When you run pub build (or pub build example when the code is not in web directory) this code modifications (and some more like tree-shaking and minification) are applied and you can open the resulting output (from build/example/example_01.html) directly without a server using a file URL.



回答2:

I think the point is both using "import html" file, which can run with web server and some browsers,, for example route run smoothly at Safari browser without web server, but did not run without server at Chrome, and Chrome shows this error msg:

Imported resource from origin 'file://' has been blocked from loading by 
Cross-Origin Resource Sharing policy: Received an invalid response. Origin 'null'
is therefore not allowed access.