angular - ViewChild 错误:预期 2 个参数,但得到 1.ts(2554) core.d.ts(8054, 47):未提供“opts”的参数
问题描述
在以下代码的这一行中,@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();
}
}
解决方案
如果您使用 angular8,则ViewChild()
装饰器已更改:https://angular.io/guide/static-query-migration,因此您缺少static
参数
@ViewChild('myListView', {static: false}) listViewComponent: RadListViewComponent;
推荐阅读
- html - Carbon Design Systems UI Shell 组件在代码笔中不起作用
- python - 使用 GPU 训练 XGBoost 回归器时内核死机
- c - 无法调用函数而不陷入无限循环
- python - 上一个作业结束后如何添加调度程序作业
- python - 只能是存储在 pygame.sprite.Group 中的矩形对象
- javascript - 如何创建一个 javascript 函数,将用户输入的输入值组合起来并将它们放在一个短语中?
- node.js - 节点是否有任何进程间内存存储?
- protocol-buffers - 在 protobuf 文件中保留 //go:generate 注释
- react-native - 移动下一个屏幕时如何删除标题的动画?
- vba - DoCmd.FindRecord 揭示了什么以及如何获得它?