简体   繁体   English

将 JavaScript 转换为 TypeScript 时出现的问题

[英]Issues while converting JavaScript to TypeScript

I've got a huge SCSS style-sheet with some JS files related to it from the UI team in my current project.我有一个巨大的 SCSS 样式表,其中包含我当前项目中 UI 团队提供的一些与之相关的 JS 文件。 My task is to convert (reuse) the JS code they created to TypeScript (as the app is in Angular).我的任务是将他们创建的 JS 代码转换(重用)为 TypeScript(因为应用程序在 Angular 中)。

Please give me advice, about what is needed to be done to the code below to make it possible.请给我建议,关于需要对下面的代码做些什么才能使其成为可能。 No need to rewrite the whole code for me;无需为我重写整个代码; if you can just point out what actions have to be done and/or share some references, you will save my life.如果您能指出必须执行的操作和/或分享一些参考资料,您将挽救我的生命。

As for now, I've already added property declarations and tried to change this.element.querySelector to document.querySelector and so on, but nothing is working as for now…至于现在,我已经添加了属性声明并尝试将this.element.querySelector更改为document.querySelector等等,但目前没有任何效果……

The whole code (without animations & before conversion) is:整个代码(没有动画和转换前)是:

class ChangeButton {
constructor(element) {
  this.element = element;
  this.container = this.element.querySelector('.switch-box-contents');
  this.content = this.element.querySelectorAll('.switch-box-content');
  this.on = this.container.querySelectorAll('.switch-box-content')[0];
  this.off = this.container.querySelectorAll('.switch-box-content')[1];

  this.ChangeButton = this.element.querySelector('.switch-button');
  this.ChangeButtonContainer = this.ChangeButton.querySelector('.switch-button-contents');
  this.textOn = this.element.querySelectorAll('.switch-button-txt')[0];
  this.textOff = this.element.querySelectorAll('.switch-button-txt')[1];

  this.checkbox = this.element.querySelectorAll('.switch-box-content-input');

  this.ngOnInit();
  this.bind();
}

ngOnInit() {
  const height = this.on.clientHeight;
  this.container.style.height = `${height}px`;
  this.off.classList.remove('-hidden');

  const txtHeight = this.textOn.clientHeight;
  this.ChangeButtonContainer.style.height = `${txtHeight}px`;
  this.textOff.classList.remove('-hidden');
}

contentOff() {
  const height = this.on.clientHeight;
  this.on.classList.remove('-active');
  this.off.classList.add('-active');

  anime({
...
  });

  const containerHeight = this.off.clientHeight;
  anime({
...
  });

  const txtHeight = this.textOn.clientHeight;
  anime({
...
  });
}

contentOn() {
  this.on.classList.add('-active');
  this.off.classList.remove('-active');
  anime({
...
  });

  const containerHeight = this.on.clientHeight;
  anime({
...
  });

  anime({
...
  });
}

getCheckbox() {
  const content = this.container.querySelectorAll('.switch-box-content');
  const activeContent = [].map.call(content, (element) => {
    return element;
  }).filter((element) => {
    return element.classList.contains('-active')
  })[0];
  return activeContent.querySelector('.switch-box-content-input');
}

check(currentCheckbox) {
  if(this.element.classList.contains('-select')) {
    currentCheckbox.checked = false;
    this.element.classList.remove('-checked')
  }
}

handleEvent(e) {
  const on = this.ChangeButton.classList.contains('-on');
  switch (e.type) {
    case 'click' :
      const checkbox = this.getCheckbox();
      if(on) {
        this.ChangeButton.classList.remove('-on');
        this.contentOff();
      } else {
        this.ChangeButton.classList.add('-on');
        this.contentOn();
      }
      this.check(checkbox);
      break;
    case 'change' :
      const chekbox = this.getCheckbox();
      if(chekbox.checked) {
        this.element.classList.add('-checked')
      } else {
        this.element.classList.remove('-checked')
      }
      break;
  }
}

bind() {
  this.content.forEach((content) => {
    content.addEventListener('click', () => {})
  });
  this.ChangeButton.addEventListener('click', this, false);
  this.checkbox.forEach((checkbox) => {
    checkbox.addEventListener('change', this, false);
  })
}
}

Looks like a large part of this is class based and added / removed on user click.看起来其中很大一部分是基于类的,并在用户点击时添加/删除。 I would add booleans in your typescript file for each of these and then use [ngClass]="boolean ? 'yourClassName' : ''" .我会在你的打字稿文件中为每一个添加布尔值,然后使用[ngClass]="boolean ? 'yourClassName' : ''" You can then add click event listeners to toggle the booleans - which in turn will toggle your classes.然后,您可以添加单击事件侦听器来切换布尔值 - 这反过来将切换您的类。

For example :例如 :

<div [ngClass]="boolean ? 'active' : 'inactive'" (click)="boolean = !boolean"></div>

The same kind of thing can be done with style and individual style attributes.同样的事情可以用样式和个人样式属性来完成。

<div [style.height]="boolean ? '10px' : '5px'" (click)="boolean = !boolean"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM