[英]Detecting if a page is using Angular 2+ with JavaScript
I am using Google Polymer on top of an Angular 2 app and I need a way in the Polymer JavaScript to determine if the page in running Angular 2 or not. 我在Angular 2应用程序上使用Google Polymer,并且需要Polymer JavaScript中的一种方法来确定页面是否正在运行Angular 2。 I had read somewhere that
if (window.ng) {}
works and it has for me up until yesterday. 我曾在某处读到
if (window.ng) {}
有效,直到昨天才对我有用。 Now, it works in localhost
but has stopped working on my test and production servers. 现在,它可以在
localhost
运行,但已停止在我的测试和生产服务器上工作。 Now, window.ng
is undefined. 现在,未定义
window.ng
。
When I debug the Polymer JavaScript and step through it slowely, I get the following console errors: 当我调试Polymer JavaScript并缓慢地通过它时,出现以下控制台错误:
(WEB_PAGE context) Lazy require of app.binding did not set the binding field
(WEB_PAGE context) Lazy require of webstore.binding did not set the binding field
(WEB_PAGE context) Lazy require of runtime.binding did not set the binding field
Any help would be much appreciated! 任何帮助将非常感激!
Angular automatically adds an attribute ng-version
to the root component's tag, so you can parse that attribute's value like this assuming the root component is the first child of body
Angular会自动将
ng-version
属性添加到根组件的标签中,因此您可以像这样假设根组件是body
的第一个子元素来解析该属性的值
const majorVersion = parseInt(document.body.children[0].getAttribute('ng-version').split('.')[0])
console.log(majorVersion >= 2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.