問題描述
當我在請求數據時返回簡單的 JSON 而沒有連接到數據庫時,Angular Universal 中的服務器渲染工作正常.
When in my requests for data I return simple JSON without connection to database the server rendering in Angular Universal works fine.
然而,我發現如果我沒有正確版本的 Zone.js,對 MySQL 數據庫的請求將不會進行.我有以下錯誤:
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.
所以我安裝了正確的 Zone.js:
So I installed proper Zone.js:
npm install zone.js@0.6.21
然后我開始從 MySQL 接收數據.
and I started receiving data from MySQL.
但是!此時服務器渲染停止工作!我只看到:
BUT! At this point server rendering stops working! I only see:
<!--template bindings={}-->
在 HTML 模板中.
in HTML-template.
我返回一個沒有連接到 MySQL 的 JSON,發現即使在這種情況下服務器渲染也不起作用.
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
然后當我在沒有連接到數據庫的情況下返回 JSON 時,服務器渲染正常工作,但是如果我嘗試連接到 MySQL,則會發生原始錯誤.
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.
所以現在我可以在沒有服務器渲染的情況下使用工作服務器渲染或連接到 MySQL.不是服務器渲染和與數據庫的工作連接.
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.
推薦答案
我找到了解決方案.就我而言,問題已通過以下命令解決:
I have found the solution. In my case the issue was resolved by the command:
npm install zone.js@latest
現在我可以向 MySQL 發出請求,而且我有一個服務器渲染,可以看到我的 HTML 模板中的所有數據.
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.
這篇關于由于 Angular Universal 服務器渲染中的 Zone.js 停止工作.怎么修?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!