简体   繁体   English

如何在angular2模板中添加JavaScript代码段

[英]How to add javascript snippet in angular2 template

I am new to angular2 development. 我是angular2开发的新手。 I would like to add the below js lines inside my footer.component.html (angular 2). 我想在footer.component.html(角度2)中添加以下js行。

But the code is not running in angular 2. Can somebody help. 但是代码不是在角度2中运行。有人可以帮忙。

<script type="text/javascript" src="http://feedjit.com/serve/?vv=1515&amp;tft=3&amp;srefs=0"></script>

<noscript><a href="http://feedjit.com/">Live Traffic Stats</a></noscript>

You can add those scripts in your index.html 您可以将这些脚本添加到index.html中

Once you add your JavaScript in the head of your index.html , your whole application will have access to it. 将JavaScript添加到index.html的头部后,整个应用程序都可以访问它。

If you need to refer to it in your components. 如果需要在组件中引用它。 You should declare a variable for it and it will become available for you. 您应该为其声明一个变量,该变量将对您可用。

For example, lets say you import some JavaScript that brings an object that you want to use and call methods on it. 例如,假设您导入了一些JavaScript,这些JavaScript带来了要使用的对象并在其上调用方法。 Let's call this object obj1. 我们将此对象称为obj1。 In your component you would declare this variable after your imports(not inside your class) 在您的组件中,您将在导入后声明此变量(不在类内部)

declare var obj1: any;

Now in your component you can use this object and call methods on it. 现在,您可以在组件中使用该对象并对其调用方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM