ViewChild错误:预期有2个参数,但是得到了1.ts(2554)core.d.ts(8054,47):未提供“ opts”的参数

时间:2019-07-12 16:07:12

标签: angular nativescript viewchild

在以下代码@ViewChild('myListView') listViewComponent: RadListViewComponent; 的这一行,我收到以下错误消息:

  

期望2个参数,但得到1.ts(2554)core.d.ts(8054,47):   未提供“选择”参数。

由于我是一名新学员,仅关注2017年的教程,所以我不知道为什么会发生这种情况以及如何解决?但是似乎ViewChild()装饰器的实现已更改,尽管我不知道如何调整我的代码以解决该问题?

import { Component, OnInit, Inject, ViewChild, ChangeDetectorRef } from '@angular/core'; 
import { View } from 'tns-core-modules/ui/core/view';
import { FavoriteService } from '../services/favorite.service'; 
import { Dish } from '../shared/dish'; 
import { ListViewEventData, RadListView } from 'nativescript-ui-listview'; 
import { RadListViewComponent } from 'nativescript-ui-listview/angular'; 
import { ObservableArray } from 'tns-core-modules/data/observable-array'; 
import { DrawerPage } from '../shared/drawer/drawer.page'; 
import { confirm } from "tns-core-modules/ui/dialogs"; 
import { Toasty, ToastDuration, ToastPosition } from 'nativescript-toasty';


@Component({ 
    selector: 'app-favorites', 
    moduleId: module.id, 
    templateUrl: './favorites.component.html', 
    styleUrls: ['./favorites.component.css'] 
}) 
export class FavoritesComponent extends DrawerPage implements OnInit {

    favorites: ObservableArray<Dish>; 
    errMess: string;

    @ViewChild('myListView') listViewComponent: RadListViewComponent;

    constructor(private favoriteservice: FavoriteService, 
        private changeDetectorRef: ChangeDetectorRef,        
        @Inject('BaseURL') private BaseURL) { 
            super(changeDetectorRef); 
        }


    ngOnInit() { 
        this.favoriteservice.getFavorites() 
        .subscribe(favorites => this.favorites = new ObservableArray (favorites), 
        errmess => this.errMess = errmess); 
    }

    deleteFavorite(id: number) { 
        console.log('delete', id);

        let options = { 
            title: "Confirm Delete", 
            message: 'Do you want to delete Dish '+ id, 
            okButtonText: "Yes", 
            cancelButtonText: "No", 
            neutralButtonText: "Cancel" 
        };

        confirm(options).then((result: boolean) => { 
            if(result) {
                this.favorites = null;
                this.favoriteservice.deleteFavorite(id) 
                .subscribe(favorites => { 
                    //const toast = new Toasty("Deleted Dish "+ id, "short", "bottom" ); 
                    //toast.show();
                    const toasty = new Toasty({ text: "Deleted Dish "+ id })
                    .setToastDuration(ToastDuration.SHORT)
                    .setToastPosition(ToastPosition.BOTTOM)
                    //.setTextColor(new Color('white'))
                    //.setBackgroundColor('#ff9999')
                    .show();  
                    this.favorites = new ObservableArray(favorites); 
                }, 
                errmess => this.errMess = errmess);
            } 
            else { 
                console.log('Delete cancelled'); 
            }
        });
    }    

    public onCellSwiping(args: ListViewEventData) { 
        var swipeLimits = args.data.swipeLimits; 
        var currentItemView = args.object; 
        var currentView;

        if(args.data.x > 200) {

        } 
        else if (args.data.x < -200) {
        }
    }

    public onSwipeCellStarted(args: ListViewEventData) { 
        var swipeLimits = args.data.swipeLimits; 
        var swipeView = args['object'];
        var leftItem = swipeView.getViewById<View>('mark-view'); 
        var rightItem = swipeView.getViewById<View>('delete-view'); 
        swipeLimits.left = leftItem.getMeasuredWidth(); 
        swipeLimits.right = rightItem.getMeasuredWidth(); 
        swipeLimits.threshold = leftItem.getMeasuredWidth()/2;
    }
    public onSwipeCellFinished(args: ListViewEventData) {
    }

    public onLeftSwipeClick(args: ListViewEventData) { 
        console.log('Left swipe click'); 
        this.listViewComponent.listView.notifySwipeToExecuteFinished(); 
    }

    public onRightSwipeClick(args: ListViewEventData) { 
        this.deleteFavorite(args.object.bindingContext.id); 
        this.listViewComponent.listView.notifySwipeToExecuteFinished(); 
    }
}

2 个答案:

答案 0 :(得分:5)

如果您使用angular8,则ViewChild()装饰器将更改为:https://angular.io/guide/static-query-migration,因此您缺少static参数 @ViewChild('myListView', {static: false}) listViewComponent: RadListViewComponent;

答案 1 :(得分:2)

在v8中,ViewChild语法已更改,CHANGELOG表示

  

“ Core:在Angular版本8中,要求所有@ViewChild和   @ContentChild查询具有“静态”标志,用于指定是否   查询是“静态”还是“动态”。编译器先前对查询进行了排序   自动,但在8.0中,开发人员必须明确   指定所需的行为。这是一个临时要求,因为   迁移的一部分;看到   https://v8.angular.io/guide/static-query-migration以获得更多详细信息。”

我建议使用https://github.com/angular/angular/blob/master/CHANGELOG.md进行更新