angular - 创建组件并在应用程序中的每个位置使用时通知文本消息不起作用
问题描述
我创建了一个组件,如下所示。该组件将显示通知,但由于某些问题,组件内容未显示。无法弄清楚我错过了什么。
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-notification',
templateUrl: './notification.component.html',
styleUrls: ['./notification.component.css']
})
export class NotificationComponent implements OnInit {
notification = {
text: '',
timeoutLimit: 7000,
serviceErrorTimeoutLimit: 10000,
normalTimeoutLimit: 7000,
assignedToTimeout: {},
isServiceError: false,
error(message: string) {
this.show('#bd362f', message);
},
serviceError(message: string) {
this.isServiceError = true;
this.timeoutLimit = this.serviceErrorTimeoutLimit;
this.show('#bd362f', message);
},
success(message: string) {
this.show('#16ad14', message);
},
info(message: string) {
this.show('#278ac3', message);
},
warning(message: string) {
this.show('#e0af00', message);
},
show(color: string, message: string) {
this.text = message;
this.toggled;
clearTimeout(this.assignedToTimeout);
document.getElementById('notificationPopup').style.backgroundColor = color;
document.getElementById('notificationPopup').style.display = 'block';
this.assignedToTimeout = setTimeout(() => {
this.clear();
}, this.timeoutLimit);
},
clear() {
document.getElementById('notificationPopup').style.display = 'none';
this.isServiceError = false;
this.timeoutLimit = this.normalTimeoutLimit;
}
};
constructor() {}
ngOnInit() {}
}
HTML是:
<div class="notification-popup" id="notificationPopup">
<span>{{ notification.text }}</span>
</div>
我在 header.component.html 中使用
<app-notification></app-notification>
header.component.ts
import { NotificationComponent } from '../common/components/notification/notification.component';
constructor(
private notification: NotificationComponent
) {
// this.productCodesList=this.productCodesList1;
this.selectedProductCode == '20201';
// this.productCodesList=this.productCodesList1;
this.vehicleTypeList = this.twoWheelertype;
this.selectedType = 'KFZK120';
this.getVehicleMake();
}
this.notification.notification.success('Data is deleted');
现在弹出通知出现但不显示文本“数据已删除”。
解决方案
不应该这样吗?
<app-notification [(toggled)]="username"></app-notification>
只是
<app-notification [toggled]="username"></app-notification>
推荐阅读
- php - Laravel 路线不适用于特定情况
- java - 将很长的 jtable 打印到多个页面中
- java - 如何解决 java.lang.ClassNotFoundException:org.springframework.security.core.context.SecurityContextHolder
- pyspark - 将 pyspark DF 写入 Redshift
- jquery - 使用父尺寸作为图像属性
- eclipse - 为 TreeViewerColumn 添加监听器
- angular - 孙子组件和父组件之间的两种绑定方式
- c - 图形表示 - 链表的链表
- android - 如何停止对话将密码保存到 Google?
- java - 使用更快的xml.jackson 使用重载的setter 反序列化为对象