如何将脚本放到角度组件

时间:2022-12-06 09:44:37

I have some project on Angular CLI. So I need to put some scripts to a component but it seems they arent there

我在Angular CLI上有一些项目。所以我需要将一些脚本放到组件中,但似乎它们不在那里

my code in the component:

我在组件中的代码:

    <script src="//vk.com/js/api/openapi.js" type="text/javascript"></script>
<div id="login_button" ></div>
<script language="javascript ">
    VK.init({
      apiId: 6241489
    });
    /*function authInfo(response) {
      if (response.session) {
        alert('user: '+response.session.mid);
        } else {
        alert('not auth');
        }
    }
    VK.Auth.getLoginStatus(authInfo);*/
    VK.UI.button('login_button');
    </script>

Who know how to fix it?

谁知道如何修复它?

2 个解决方案

#1


0  

First, load the third-party script by adding it to the apps[0].scripts property in .angular-cli.json as described in Angular CLI doc.

首先,通过将第三方脚本添加到.angular-cli.json中的apps [0] .scripts属性来加载第三方脚本,如Angular CLI doc中所述。

Then, make the global VK object accessible in your component by definining declare var VK: any; and call VK.init() from your component's ngOnInit(). I assume that will be the AppComponent.

然后,通过定义declare var VK:any来使组件中的全局VK对象可访问;并从组件的ngOnInit()中调用VK.init()。我假设这将是AppComponent。

#2


0  

I don't believe you're supposed to create script tags in your component.html files.

我不相信你应该在component.html文件中创建脚本标签。

If you want to fire the supposed function, try this.

如果你想激活假定的函数,试试这个。

.ts file:

.ts文件:

authInfo() {
    // Where does response params come from?
    if (response.session) {
        alert('user: '+response.session.mid);
    } else {
        alert('not auth');
    }
}

.html template:

.html模板:

<button (click)="authInfo()"></button>

Please reply to this answer with more detail so I can assist you further :)

请回复此答案更详细,以便我可以进一步帮助你:)

#1


0  

First, load the third-party script by adding it to the apps[0].scripts property in .angular-cli.json as described in Angular CLI doc.

首先,通过将第三方脚本添加到.angular-cli.json中的apps [0] .scripts属性来加载第三方脚本,如Angular CLI doc中所述。

Then, make the global VK object accessible in your component by definining declare var VK: any; and call VK.init() from your component's ngOnInit(). I assume that will be the AppComponent.

然后,通过定义declare var VK:any来使组件中的全局VK对象可访问;并从组件的ngOnInit()中调用VK.init()。我假设这将是AppComponent。

#2


0  

I don't believe you're supposed to create script tags in your component.html files.

我不相信你应该在component.html文件中创建脚本标签。

If you want to fire the supposed function, try this.

如果你想激活假定的函数,试试这个。

.ts file:

.ts文件:

authInfo() {
    // Where does response params come from?
    if (response.session) {
        alert('user: '+response.session.mid);
    } else {
        alert('not auth');
    }
}

.html template:

.html模板:

<button (click)="authInfo()"></button>

Please reply to this answer with more detail so I can assist you further :)

请回复此答案更详细,以便我可以进一步帮助你:)