问题
When in my requests for data I return simple JSON without connection to database the server rendering in Angular Universal works fine.
However I have found that requests to MySQL database is not going if I don't have a proper version of Zone.js. I had the following error:
TypeError: Cannot read property 'on' of undefined
at Protocol._enqueue (/Applications/MAMP/htdocs/Angular2/myproject/node_modules/mysql/lib/protocol/Protocol.js:152:5)
...
Also I payed attention that I had a warning:
angular2-universal@2.1.0-rc.1 requires a peer of zone.js@~0.6.21 but none was installed.
So I installed proper Zone.js:
npm install zone.js@0.6.21
and I started receiving data from MySQL.
BUT! At this point server rendering stops working! I only see:
<!--template bindings={}-->
in HTML-template.
I moved back to return a JSON without connection to MySQL and have found that even at this case server rendering is not working.
So I have played a little with this and have found that if I use command:
npm install zone.js
then server rendering works properly when I return JSON without connection to database, but if I try to connect to MySQL then original error occurs.
So now I have either working server rendering or connection to MySQL without server rendering. Not server rendering and working connection to database.
If anyone knows what should be done I'll appreciate the help. Thank you.
回答1:
I have found the solution. In my case the issue was resolved by the command:
npm install zone.js@latest
Now I can do the requests to MySQL and also I have a server rendering and can see all the data in my HTML-template.
来源:https://stackoverflow.com/questions/42659236/due-to-zone-js-in-angular-universal-server-rendering-stops-working-how-to-fix