如何在Angular中的自定义窗口对象上使用Object.defineProperty?

时间:2019-06-05 11:09:03

标签: javascript angular typescript window

例如,我将YOTI对象注入了浏览器:

当我在控制台中运行时:

window.YOTI

我得到:

{onShare: ƒ, redirect: ƒ}

然后,如果我访问重定向方法,我会得到:

window.YOTI.redirect
ƒ (callbackUrl, token) {
                var currentQuery =  window.location.search.replace('?','')
                var currentFragment = window.location.hash
                var separator = (callbackU…

我想在angular onInit()中修改此属性,并从这里(https://yoti.zendesk.com/hc/en-us/articles/360008958873-Does-the-Yoti-frontend-client-support-an-Angular-App-)尝试了此操作:

const ytcontainer = {
};

Object.defineProperty(window, 'YOTI', {
  get: () => ytcontainer.YOTI,

  set: yoti => {
    ytcontainer.YOTI = yoti;

    Object.defineProperty(yoti, 'redirect', {
      get: () => {
        return (callbackUrl, token) => {
          console.log(callbackUrl);
          console.log(token);
        };
      },
    });
  },
});

但是在那之后,当我运行window.YOTI时,我得到了未定义的对象。

window.YOTI
undefined

我在这里做什么错了?

0 个答案:

没有答案