代码之家  ›  专栏  ›  技术社区  ›  Rolando

为什么这个应用转换组和应用转换组项自定义组件在angular 8中不起作用(与angular 2一起工作)?

  •  0
  • Rolando  · 技术社区  · 5 年前

    我在angular 8中创建了一个自定义组件,可以对一些列表项进行洗牌。我总是得到错误:

    Can't bind to 'app-transition-group' since it isn't a known property of 'ul'. 
    

    我怎样才能绕过这个问题?

    我在angular 8应用程序中有一个transition-group.ts文件:

    import { Component, ContentChildren, Directive, ElementRef, Input, QueryList } from '@angular/core';
    
    @Directive({
      selector: '[app-transition-group-item]'
    })
    export class TransitionGroupItemDirective {
      prevPos: any;
      newPos: any;
      el: HTMLElement;
      moved: boolean;
      moveCallback: any;
    
      constructor(elRef: ElementRef) {
        this.el = elRef.nativeElement;
      }
    }
    
    @Component({
      selector: '[app-transition-group]',
      template: '<ng-content></ng-content>'
    })
    export class TransitionGroupComponent {
      @Input('app-transition-group') class;
    
      @ContentChildren(TransitionGroupItemDirective) items: QueryList<TransitionGroupItemDirective>;
    
      ngAfterContentInit() {
        this.refreshPosition('prevPos');
        this.items.changes.subscribe(items => {
          items.forEach(item => {
            item.prevPos = item.newPos || item.prevPos;
          });
    
          items.forEach(this.runCallback);
          this.refreshPosition('newPos');
          items.forEach(this.applyTranslation);
    
          // force reflow to put everything in position
          const offSet = document.body.offsetHeight;
          this.items.forEach(this.runTransition.bind(this));
        })
      }
    
      runCallback(item: TransitionGroupItemDirective) {
        if(item.moveCallback) {
          item.moveCallback();
        }
      }
    
      runTransition(item: TransitionGroupItemDirective) {
        if (!item.moved) {
          return;
        }
        const cssClass = this.class + '-move';
        let el = item.el;
        let style: any = el.style;
        el.classList.add(cssClass);
        style.transform = style.WebkitTransform = style.transitionDuration = '';
        el.addEventListener('transitionend', item.moveCallback = (e: any) => {
          if (!e || /transform$/.test(e.propertyName)) {
            el.removeEventListener('transitionend', item.moveCallback);
            item.moveCallback = null;
            el.classList.remove(cssClass);
          }
        });
      }
    
      refreshPosition(prop: string) {
        this.items.forEach(item => {
          item[prop] = item.el.getBoundingClientRect();
        });
      }
    
      applyTranslation(item: TransitionGroupItemDirective) {
        item.moved = false;
        const dx = item.prevPos.left - item.newPos.left;
        const dy = item.prevPos.top - item.newPos.top;
        if (dx || dy) {
          item.moved = true;
          let style: any = item.el.style;
          style.transform = style.WebkitTransform = 'translate(' + dx + 'px,' + dy + 'px)';
          style.transitionDuration = '0s';
        }
      }
    }
    

    在我的app.module.ts文件中,我有:

    import { TransitionGroupComponent, TransitionGroupItemDirective } from './transition-group';
    

    并确保在声明中包含TransitionGroupImponent和TransitionGroupItemDirective。

    在我的app.component.ts文件中,我有:

    @Component({
      selector: 'my-app',
      template: `
        <h2>Items reorder/shuffle animations with Angular8 ngFor</h2>
        <button (click)="shuffle()">Configure</button>
        <ul [transition-group]="'flip-list'">
          <li *ngFor="let item of items" transition-group-item>
            {{ item }}
          </li>
        </ul>
      `,
    })
    export class App {
      items = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    
      shuffle() {
        this.items = this.shufflestuff(this.items)
      }
    }
    
    shufflestuff=function(array) {
      const length = array == null ? 0 : array.length
      if (!length) {
        return []
      }
      let index = -1
      const lastIndex = length - 1
      const result = this.copyArray(array)
      while (++index < length) {
        const rand = index + Math.floor(Math.random() * (lastIndex - index + 1))
        const value = result[rand]
        result[rand] = result[index]
        result[index] = value
      }
      return result
    }
    
    copyArray=function(source, array) {
      let index = -1
      const length = source.length
    
      array || (array = new Array(length))
      while (++index < length) {
        array[index] = source[index]
      }
      return array
    }
    
    0 回复  |  直到 5 年前
        1
  •  0
  •   slaesh    5 年前

    请参阅工作现场演示: https://stackblitz.com/edit/angular-so-1?file=src/app/app.component.ts

    更改为:

    @Directive({
      selector: '[transition-group]'
    })
    export class TransitionGroupComponent {
      @Input('transition-group') class;
      // ...
    }
    
    推荐文章