2016-11-03 33 views
0

我正嘗試在Angular2中嵌入一個「Get Your Guide」小部件。嵌入在Angular2組件中獲取您的指南小部件

我可以在index.html中添加腳本並獲取請求,但我不知道如何將其添加到組件的HTML中。

<script async defer src="//widget.getyourguide.com/v2/core.js" onload="GYG.Widget(document.getElementById('gyg-widget'),{'q':'Paris'});"></script> 

我在HTML嘗試:

<div id="gyg-widget"></div> 

而在打字稿:

import { Component, OnInit } from '@angular/core'; 

declare var GYG: any; 

@Component({ 
    selector: 'app-excursions', 
    templateUrl: './excursions.component.html', 
    styleUrls: ['./excursions.component.scss'] 
}) 

export class ExcursionsComponent implements OnInit { 

    constructor() {} 

    ngOnInit() { 
    new GYG.Widget(document.getElementById('gyg-widget'),{'q':'Paris'}); 
    } 
} 

但是,這是純粹的猜測,並沒有工作。

錯誤:錯誤./ExcursionsComponent類ExcursionsComponent_Host - 聯模板:0:0引起的:未定義GYG 的ReferenceError:GYG不是在ExcursionsComponent.ngOnInit定義 (excursions.component.ts:20)

我需要做什麼?

回答

0

我使用角CLI和我已經

  1. 解決了這個問題,從下載的widget.getyourguide.com/v2/core.js讓您的指南庫並把它作爲LIB/GYG。 JS

  2. 添加以下行角cli.json:

    「腳本」:[ 「LIB/gyg.js」 ],

這取代了上面的部分。按照Get Your Guide的建議,我無法直接從外部服務器加載腳本。