【问题标题】:"ion-slides "options" has been deprecated", now what?“离子幻灯片”选项“已被弃用”,现在怎么办?
【发布时间】:2017-06-19 06:26:00
【问题描述】:

我正在尝试实现Ionic2's Slides,但我找不到如何设置设置:

ion-slides“选项”已被弃用。请改用 ion-slide 的输入属性。

我可以找到“输入属性”(例如 自动播放、速度、方向),但我不知道将其放置在哪里。我找到的每个示例都是[options]="slideOptions",其中slideOptions 是设置,但除了已弃用的通知之外,不会产生任何结果。

我是 ionic v2 和 typescript 的新手,我可能会得到一个可行的解决方案,但我想把它做好。


ion-content 中的 HTML:

<ion-slides [options]="slideOptions">
    <ion-slide *ngFor="let item of items">
        <img width="20%"src="{{item.thumb.source}}">
    </ion-slide>
</ion-slides>

还有简化的类: 从“离子角度”导入{幻灯片};

@Component({
  selector: 'page-example',
  templateUrl: 'example.html'
})

export class Example {
    public items: any;
    private slideOptions: any;

    @ViewChild(Slides) slides: Slides;

    constructor(private navCtrl: NavController, public navParams: NavParams) {
        this.items = [];
        this.albumInfo = navParams.get('item');

        this.getSlides();
    }

    // This does nothing:
    goToSlide() {
        this.slides.slideTo(2, 500);
    }
    // This  does nothing:
    ngAfterViewInit() {
        this.slides.autoplay = 2000;
    }


    // Simple example of getting the slides
    makeGetRequest():Promise<string> {      
        /* Get the items code, populates this.items
    }
}

【问题讨论】:

    标签: typescript ionic2 ion-slides


    【解决方案1】:

    离子幻灯片已更改。确保您已更新到最新的 ionic 版本 (2.0) site 中列出的输入属性可以直接在 html 中使用。例如:

    <ion-slides pager loop autoplay="2000">
     <ion-slide> ... </ion-slide>
    </ion-slides>
    

    要使用输入属性中未列出的属性,您可以在 HTML 中使用变量。

    <ion-slides #slides> 
       ...
    </ion-slides>
    

    并在 TS 中使用它:

     import { Component, ViewChild } from '@angular/core';
     import {Slides} from 'ionic-angular'
    
    export class Example {
        @ViewChild(Slides) slides: Slides;
        constructor() {}
        ngAfterViewInit() {
            this.slides.autoplay = 2000;
            this.slides.autoplayDisableOnInteraction = false;
       }
    }
    

    在您的情况下,ngAfterViewInit 什么都不做,因为您没有在 html 中将变量定义为 &lt;ion-slides #slides&gt;

    【讨论】:

    • 我已经实现了你的代码,并且得到“无法读取未定义的属性'hasAttribute'”。我可以更改所有设置(至少我尝试过的设置),但无法设置自动播放。哪个是我想要的。感谢您的回复
    • 还是不行..你更新了你的离子版本了吗?
    • 一切正常,只有自动播放不行。自动播放返回错误,其他设置正常。我正在使用当前的最新版本 2.1.4 :)
    • 但它对我有用,没有问题。你能用你最新的代码更新你的问题吗?
    • 确实和题目很像。我发现这可能是因为 init 上没有幻灯片,所以我将autoplay 移动到在makeGetRequest() 承诺完成后设置。它不起作用,但它也不再给出错误
    【解决方案2】:

    输入属性表示ion-slides的html中设置的属性,输出事件是html事件。 例如:

    <ion-slides [autoplay]="num">
        <ion-slide *ngFor="let item of items">
            <img width="20%"src="{{item.thumb.source}}">
        </ion-slide>
    </ion-slides>
    

    其中num 是组件中具有特定数值的变量。

    【讨论】:

    • 我试过这个,导致“无法读取未定义的属性'hasAttribute'”。我已经使用了我在主题中的代码,并在没有所有幻灯片代码的情况下尝试了它(所以只是 html 变体。两者都会导致这个错误
    • 我发现了更多您发布的建议,但这些建议不再正常工作,沿途某处发生了相当无证的更改:)
    • github.com/driftyco/ionic/blob/master/… ionic 在 RC5 的幻灯片中发生了重大变化。也许这就是所有这些错误的开始......
    猜你喜欢
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多