在 Angular 组件的构造函数中放置任何内容都会导致整个页面失败
Putting anything in the constructor of Angular component results in failure of entire page
我正在尝试向我的 Angular 组件之一的构造函数添加一些内容,但是,每当我在构造函数中添加一些内容时,它都会使整个页面变为空白 - 删除所有其他组件并且只显示背景。
例如 - 这会起作用。
TS
@Component({
selector: 'app-cardboxes',
templateUrl: './cardboxes.component.html',
styleUrls: ['./cardboxes.component.scss']
})
export class CardboxesComponent implements OnInit {
constructor() { }
ngOnInit(): void {}
}
这将呈现完全空白 - 删除所有内容,甚至其他组件。
TS
@Component({
selector: 'app-cardboxes',
templateUrl: './cardboxes.component.html',
styleUrls: ['./cardboxes.component.scss']
})
export class CardboxesComponent implements OnInit {
constructor(private dialog: MatDialog) { }
ngOnInit(): void {}
}
郑重声明,在构造函数中放入什么并不重要 - 每次都是相同的结果。 Chrome 终端说有一个 NullInjectorError - 没有 MatDialog
的提供者
这有什么原因吗,或者有什么简单的解决办法吗?我不明白为什么会这样,我真的需要能够使用构造函数。我必须在某处进行另一次导入吗?是否缺少我的配置?
在构造函数中添加的项目应该在模块中导入
您正在尝试使用 angular 材料。请在控制台中使用此命令。
npm 我@angular/material
打开 app.module 和
NgModule ({....
进口:[...,
MatSliderModule,
…]
我正在尝试向我的 Angular 组件之一的构造函数添加一些内容,但是,每当我在构造函数中添加一些内容时,它都会使整个页面变为空白 - 删除所有其他组件并且只显示背景。
例如 - 这会起作用。
TS
@Component({
selector: 'app-cardboxes',
templateUrl: './cardboxes.component.html',
styleUrls: ['./cardboxes.component.scss']
})
export class CardboxesComponent implements OnInit {
constructor() { }
ngOnInit(): void {}
}
这将呈现完全空白 - 删除所有内容,甚至其他组件。
TS
@Component({
selector: 'app-cardboxes',
templateUrl: './cardboxes.component.html',
styleUrls: ['./cardboxes.component.scss']
})
export class CardboxesComponent implements OnInit {
constructor(private dialog: MatDialog) { }
ngOnInit(): void {}
}
郑重声明,在构造函数中放入什么并不重要 - 每次都是相同的结果。 Chrome 终端说有一个 NullInjectorError - 没有 MatDialog
的提供者这有什么原因吗,或者有什么简单的解决办法吗?我不明白为什么会这样,我真的需要能够使用构造函数。我必须在某处进行另一次导入吗?是否缺少我的配置?
在构造函数中添加的项目应该在模块中导入
您正在尝试使用 angular 材料。请在控制台中使用此命令。 npm 我@angular/material 打开 app.module 和
NgModule ({.... 进口:[..., MatSliderModule, …]