获取未定义的错误,即使在angular中声明变量为declare var xyz
看起来您正在声明变量获取未定义的错误,即使在angular中声明变量为declare var xyz,angular,typescript,chat,Angular,Typescript,Chat,看起来您正在声明变量WhWidgetSendButton,但从未对其进行设置 本质上,WhWidgetSendButton=undefined,然后您试图对未定义的变量调用.init(主机、协议、选项) 调用init之前,需要将WhWidgetSendButton设置为类似WhWidgetSendButton=new WhWidgetSendButton()的值,错误为“WhWidgetSendButton未定义”,与未定义的变量WhWidgetSendButton不同,它在代码中的任何地方都不
WhWidgetSendButton
,但从未对其进行设置
本质上,WhWidgetSendButton=undefined
,然后您试图对未定义的变量调用.init(主机、协议、选项)
调用
init
之前,需要将WhWidgetSendButton
设置为类似WhWidgetSendButton=new WhWidgetSendButton()
的值,错误为“WhWidgetSendButton未定义”,与未定义的变量WhWidgetSendButton不同,它在代码中的任何地方都不会被设置——它看起来像是您试图利用的库,但var WhWidgetSendButton代码>将其设置为未定义。这样想吧<代码>变量x
x.doSomething()
永远不会工作,因为x
从未被实例化(或定义)。不过,这是可行的:varx=newsomeclass();x、 doSomething()
因为定义了x
。
declare
var WhWidgetSendButton;
ngOnInit() {
var options = {
facebook: "xxxx", // Facebook page ID
whatsapp: "xxx", // WhatsApp number
call_to_action: "Message us", // Call to action
button_color: "#008000", // Color of button
position: "left", // Position may be 'right' or 'left'
order: "whatsapp,facebook", // Order of buttons
};
var proto = document.location.protocol;
var host = "whatshelp.io";
var url = proto + "//static." + host;
let s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = url + '/widget-send-button/js/init.js';
s.onload = () => {
WhWidgetSendButton.init(host, proto, options);
};
let x = document.getElementsByTagName('script')[0];
x.parentNode.insertBefore(s, x);
}