我正在运行一个现有的 Ionic 2 项目(2.0.0-beta.11,使用 Angular 2.0.0-rc.4)。 安装和设置,但不断收到以下错误(运行时发生错误。编译时没有问题)
冉Npm:
$ npm install ng2-lazyload-image --save
导入到我的组件中:
import { LazyLoadImageDirective } from 'ng2-lazyload-image';
设置指令:
directives: [ LazyLoadImageDirective ]
设置变量和默认值:
export class PostComponent {
defaultImage: any = 'build/assets/preloader.gif';
offset = 100;
@Input() feedImage: string = 'build/assets/blank-default-feed-bg.png';
}
在我的模板中:
<img [src]="defaultImage" [lazyload]="feedImage" [offset]="offset" class="feed-image">
也许我忽略了一些东西,但我似乎看不到它是什么。
你好,
我无法重现该问题。 您使用的是什么版本的 ng2-lazyload-image? 你的项目是开源的,我可以看看吗?
$ npm install -g ionic<strong i="8">@beta</strong>
$ ionic --version
2.0.0-beta.37
$ ionic start ionic-lazy-load-images --v2
$ cd ionic-lazy-load-images
$ npm install [email protected] --save
$ ionic serve
`app/pages/home/home.html:
<ion-header>
<ion-navbar>
<ion-title>Home</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding class="home" #container>
<h2>Welcome to Ionic!</h2>
<p>
This starter project comes with simple tabs-based layout for apps
that are going to primarily use a Tabbed UI.
</p>
<p style="height: 1000px;">
Take a look at the <code>app/</code> directory to add or change tabs,
update any existing page or create new pages.
</p>
<img
style="height: 1099px;"
[src]="defaultImage"
[lazyLoad]="image"
[offset]="offset"
[scrollTarget]="container._scroll._el">
</ion-content>
`app/pages/home/home.ts:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { LazyLoadImageDirective } from 'ng2-lazyload-image';
@Component({
templateUrl: 'build/pages/home/home.html',
directives: [ LazyLoadImageDirective ]
})
export class HomePage {
defaultImage = 'https://www.placecage.com/1000/1000';
image = 'https://hd.unsplash.com/photo-1441765425173-8fd330fb4a02';
offset = 100;
constructor(private navCtrl: NavController) {
}
}
它工作正常:)
但是,由于 ionic 使用选项卡(默认情况下)并在滚动容器上添加display: none
,因此几乎不可能确定图像最初是否在视口中。 使用像intersection Observer这样的东西应该很好,但它只适用于Chrome,如果你针对iOS,你将使用Safari:/
然而,一种解决方案是检查图像是否隐藏(例如: el.offsetParent === null
)但是当图像再次可见时, ng2-lazyload-image
将不会收到任何通知,如果图像在视口中,则在用户开始滚动之前,图像不会显示。 但是,您可以为此使用*ngIf
:
<img
*ngIf="container._scroll._el.offsetParent !== null"
style="height: 1099px;"
[src]="defaultImage"
[lazyLoad]="image"
[offset]="offset"
[scrollTarget]="container._scroll._el">
但这看起来不太好:/
所以..你可能会或可能无法使用ng2-lazyload-image
在您的应用程序离子(取决于您的布局),但你不应该得到任何运行时错误。
你打错字了。
它是 [lazyLoad],而不是 [lazyload]
干杯:)
这听起来简单得令人尴尬。
我将升级到 rc1 并尝试一下。 谢谢。
在星期一,2016年10月17日,在下午1:37,斯坦FAAS [email protected]写道:
你打错字了。
它是 [lazyLoad],而不是 [lazyload]干杯:)
—
您收到此消息是因为您创作了该线程。
直接回复本邮件,在GitHub上查看
https://github.com/tjoskar/ng2-lazyload-image/issues/37#issuecomment -254195387,
或静音线程
https://github.com/notifications/unsubscribe-auth/APPh0par6NR7EA3DA1GbibncX8gCb2YRks5q02wTgaJpZM4J5EjU
.
@StanFaas ,不错的收获!
我遇到了同样的错误,我该如何解决。
最有用的评论
你打错字了。
它是 [lazyLoad],而不是 [lazyload]
干杯:)