【问题标题】:Using Packery with Angular 2在 Angular 2 中使用 Packery
【发布时间】:2017-02-09 15:23:08
【问题描述】:

我似乎无法让 Packery 使用 angular-cli 和 typescript 2.0 与 Angular 2 一起工作。 我正在使用 Packery 1.4.1 和该版本的打字稿定义。

问题是在使用 ng serve 运行项目时,找不到对 Outlayer 的 Packery 引用。

具体来说会抛出以下异常:

TypeError:无法设置未定义的属性“位置” 在 Object.utils.extend (eval at webpackJsonp.82.module.exports

下面是我的项目代码。

来自 angular-cli.json 的脚本部分:

"scripts": [
        "../node_modules/packery/dist/packery.pkgd.js"],

我也试过添加依赖的js文件,但是抛出的异常是一样的:

"scripts": [
        "../node_modules/packery/dist/packery.pkgd.js",
        "../node_modules/get-size/get-size.js",
        "../node_modules/outlayer/outlayer.js",
        "../node_modules/ev-emitter/ev-emitter.js",
        "../node_modules/fizzy-ui-utils/utils.js",
        "../node_modules/desandro-matches-selector/matches-selector.js"
        ],

app.component.ts:

import { Component, ViewChild, AfterViewInit } from '@angular/core';
declare var Packery: any;

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit
{
  @ViewChild('grid') grid;

  private pckry: any;
  constructor() {}

  ngAfterViewInit(){
    this.pckry = new Packery(this.grid, {
      itemSelector: '.grid-item'});
  }
}

app.component.html

<div #grid class="grid">
  <div class="grid-item"></div>
  <div class="grid-item"></div>
</div>

我想帮助我了解如何让 Packery 在 Angular 2 中运行,无论是否有类型(vanilla js 对我来说都可以)。

【问题讨论】:

    标签: angular webpack angular-cli packery


    【解决方案1】:

    上面的问题是我没有使用网格的“nativeElement”属性。这将起作用:

    var packery = new Packery(this.grid.nativeElement, {
    itemSelector: '.grid-item', columnWidth: 100 });
    

    此外,angular-cli 的脚本部分所需的唯一脚本如下:

    "scripts": [
            "../node_modules/packery/dist/packery.pkgd.js"        
            ]
    

    要添加可拖动性,请使用“npm install draggability --save”并将另一个脚本添加到 angular-cli 的脚本部分:

    "scripts": [
            "../node_modules/packery/dist/packery.pkgd.min.js",
            "../node_modules/draggabilly/dist/draggabilly.pkgd.min.js"        
            ]
    

    然后更新组件类:

    import { Component, ViewChild, AfterViewInit } from '@angular/core';
    declare var Packery: any;
    declare var Draggabilly: any;
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent implements AfterViewInit
    {      
      @ViewChild('grid') grid;   
    
      constructor() {}
    
      ngAfterViewInit(){
          var packery = new Packery(this.grid.nativeElement, {
            itemSelector: '.grid-item', columnWidth: 100 });
    
          packery.getItemElements().forEach( function( itemElem ) {
            var draggie = new Draggabilly( itemElem );
            packery.bindDraggabillyEvents( draggie );
          });         
      }
    }
    

    【讨论】:

    • 您是否曾经需要刷新网格?如果有,您是如何做到的?
    • @JeremyThomas 不幸的是,如果不重新设置每个图块的位置,我还无法刷新网格。我实际上正在考虑在拖动它们时更新后端的位置,以便它们在更新后保持在该位置。
    • 我实际上已经放弃了让 Packery 在没有 Angular 问题的情况下工作。主要问题是使用来自后端的新数据更新网格 - 它似乎从未正常工作。同样使用不直接支持 Angular 的网格也没有多大意义,因为 Angular 集成会有太多问题。因此,我使用 Dragula 重写了网格:github.com/valor-software/ng2-dragula Dragula 只是为我的用例“工作”,因为它通常可以更好地控制列和(固定)项目的定位。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 2017-11-05
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多