由于 Angular Universal 服务器渲染中的 Zone.js 停止工

Due to Zone.js in Angular Universal server rendering stops working. How to fix?(由于 Angular Universal 服务器渲染中的 Zone.js 停止工作.怎么修?)

本文介绍了由于 Angular Universal 服务器渲染中的 Zone.js 停止工作.怎么修?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在请求数据时返回简单的 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 停止工作.怎么修?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:由于 Angular Universal 服务器渲染中的 Zone.js 停止工

基础教程推荐