我似乎无法获得 Packery使用 angular-cli 和 typescript 2.0 使用 Angular 2。 我正在使用 Packery 1.4.1 和来自 DefinitelyTyped 的该版本的 typescript 定义。
问题是 Packery 对 Outlayer 的引用使用 ng serve 运行项目时找不到。
具体来说,会因此抛出以下异常:
TypeError: Cannot set property 'position' of undefined at 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"
],
应用程序组件.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 对我来说没问题)。
最佳答案
上面的问题是我没有使用网格的“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 );
});
}
}
关于angular - 在 Angular 2 中使用 Packery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42140537/