Material HTTP table example throws 属性 没有初始化器并且在本地环境的构造函数中没有明确分配
Material HTTP table example throws Property has no initializer and is not definitely assigned in the constructor in local environment
我正在尝试创建一个充满 HTTP 请求的 table,因此我从 angular material 文档中提供的示例开始 HTTP table
:
https://material.angular.io/components/table/examples
https://stackblitz.com/angular/rkmqrvxvjlk?file=src%2Fapp%2Ftable-http-example.ts
当我在网站上执行它时,它起作用了。但是当我将它添加到本地环境中的组件时,我得到了这个异常:
Property 'exampleDatabase' has no initializer and is not definitely assigned in the constructor.ts(2564)
Property 'filteredAndPagedIssues' has no initializer and is not definitely assigned in the constructor.ts(2564)
Property 'paginator' has no initializer and is not definitely assigned in the constructor.ts(2564)
Property 'sort' has no initializer and is not definitely assigned in the constructor.ts(2564)
我尝试了在变量后添加 ?
或 !
的变通方法,但它仍然不起作用。我不会看到异常,但它会在运行时崩溃:
ERROR TypeError: this.sort is undefined.
除了更改组件名称外,我没有对代码做任何更改。除此之外,我只是复制了 HTML 和 css 以及 .ts 内容和导入:
您可能忘记包含 MatSortModule
。
别忘了将其导入您的 'app.modules.ts'
import { MatSortModule } from '@angular/material/sort';
...
@NgModule({
declarations: [
...
],
imports: [
...
MatSortModule
],
...
})
export class AppModule { }
我正在尝试创建一个充满 HTTP 请求的 table,因此我从 angular material 文档中提供的示例开始 HTTP table
:
https://material.angular.io/components/table/examples
https://stackblitz.com/angular/rkmqrvxvjlk?file=src%2Fapp%2Ftable-http-example.ts
当我在网站上执行它时,它起作用了。但是当我将它添加到本地环境中的组件时,我得到了这个异常:
Property 'exampleDatabase' has no initializer and is not definitely assigned in the constructor.ts(2564)
Property 'filteredAndPagedIssues' has no initializer and is not definitely assigned in the constructor.ts(2564)
Property 'paginator' has no initializer and is not definitely assigned in the constructor.ts(2564)
Property 'sort' has no initializer and is not definitely assigned in the constructor.ts(2564)
我尝试了在变量后添加 ?
或 !
的变通方法,但它仍然不起作用。我不会看到异常,但它会在运行时崩溃:
ERROR TypeError: this.sort is undefined.
除了更改组件名称外,我没有对代码做任何更改。除此之外,我只是复制了 HTML 和 css 以及 .ts 内容和导入:
您可能忘记包含 MatSortModule
。
别忘了将其导入您的 'app.modules.ts'
import { MatSortModule } from '@angular/material/sort';
...
@NgModule({
declarations: [
...
],
imports: [
...
MatSortModule
],
...
})
export class AppModule { }