Embed Tradingview into Angular 5

白昼怎懂夜的黑 提交于 2019-12-23 10:58:09

问题


I just copy the function in the embed script found here (https://www.tradingview.com/widget/advanced-chart/) into a component in my Angular 5 app, and include the source script in angular-cli.json. However, the component returns error "Cannot find name 'TradingView'". Someone has any suggestions?


回答1:


I found a way to solve the compile error by declaring TradingView in component:

declare const TradingView: any;

export class ChartComponent implements AfterViewInit {
ngAfterViewInit() {
   new TradingView.widget({
      'container_id': 'technical-analysis',
      'autosize': true,
      'symbol': this.symbolPair,
      'interval': '120',
      'timezone': 'exchange',
      'theme': 'Dark',
      'style': '1',
      'toolbar_bg': '#f1f3f6',
      'withdateranges': true,
      'hide_side_toolbar': false,
      'allow_symbol_change': true,
      'save_image': false,
      'hideideas': true,
      'studies': [ 
      'MASimple@tv-basicstudies' ],
      'show_popup_button': true,
      'popup_width': '1000',
      'popup_height': '650'
    });
  }
}

View:

<div id="technical-analysis"></div>



回答2:


  1. I added widget loading scripts to index.html, after
  2. in component's template-file (html) I leaved only html tags
  3. in component's ts-file:

    declare const TradingView: any;

    and in ngAfterViewInit() I putted new TradingView.widget(...) inside setTimeout

And that's it! It works.




回答3:


Works fine with my implementation https://stackblitz.com/edit/stack-48296351




回答4:


just add

<script type="text/javascript" src="https://s3.tradingview.com/tv.js"></script> 

in index.html and it will be work perfectly.



来源:https://stackoverflow.com/questions/48296351/embed-tradingview-into-angular-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!