Angular 无法读取传递LocalStorage值以查看组件7的未定义属性
我试图将从localstoage接收到的值传递给我的视图组件。由于某些原因,我没有定义,但是在控制台中,我从本地存储中获取数据 我做错了什么 ModifyView.Component.TSAngular 无法读取传递LocalStorage值以查看组件7的未定义属性,angular,angular-material,Angular,Angular Material,我试图将从localstoage接收到的值传递给我的视图组件。由于某些原因,我没有定义,但是在控制台中,我从本地存储中获取数据 我做错了什么 ModifyView.Component.TS export class ModifyViewComponent { @Input() ActionMenuLines: ActionMenuLine[]; @Input() currentAction: Action; @Input() currentUser: User; c
export class ModifyViewComponent {
@Input() ActionMenuLines: ActionMenuLine[];
@Input() currentAction: Action;
@Input() currentUser: User;
close: any;
viewData: any;
viewTag: number;
action: any;
dataSource: ViewDataSource;
fetchedData: any;
primaryTableValue: any;
defaultSortCol = '1';
constructor(public dialog: MatDialog, private actionService: ActionService) {}
// tslint:disable-next-line: use-life-cycle-interface
ngOnInit() {
this.initData();
}
initData() {
const action = JSON.parse(localStorage.getItem('DbrAction'));
console.log(action);
}
}
ModifyView.Component.HTML
<div>
<mat-card class="view-settings-descrpition-card">
<mat-card-header>
</mat-card-header>
<mat-card-content>
<p>{{action.ActionName}}</p>
</mat-card-content>
</mat-card>
</div>
{{action.ActionName}
删除
const
并尝试使用this.action=JSON.parse(localStorage.getItem('DbrAction')
删除const
并尝试使用this.action=JSON.parse(localStorage.getItem('DbrAction')代码>声明一个全局类变量并按如下方式更新它:
export class ModifyViewComponent {
dbrAction: any;
// ... other parts of code
// tslint:disable-next-line: use-life-cycle-interface
ngOnInit() {
this.initData();
}
initData() {
this.dbrAction= JSON.parse(localStorage.getItem('DbrAction'));
console.log(this.dbrAction);
}
}
在HTML中使用此变量:
<div>
<mat-card class="view-settings-descrpition-card">
<mat-card-header>
</mat-card-header>
<mat-card-content>
<p> {{ dbrAction.ActionName }}</p>
</mat-card-content>
</mat-card>
</div>
{{dbrAction.ActionName}
希望它能解决您的问题…声明一个全局类变量并按如下方式更新:
export class ModifyViewComponent {
dbrAction: any;
// ... other parts of code
// tslint:disable-next-line: use-life-cycle-interface
ngOnInit() {
this.initData();
}
initData() {
this.dbrAction= JSON.parse(localStorage.getItem('DbrAction'));
console.log(this.dbrAction);
}
}
在HTML中使用此变量:
<div>
<mat-card class="view-settings-descrpition-card">
<mat-card-header>
</mat-card-header>
<mat-card-content>
<p> {{ dbrAction.ActionName }}</p>
</mat-card-content>
</mat-card>
</div>
{{dbrAction.ActionName}
希望它能解决您的问题…什么是console.log(操作)代码>登录?它正在记录存储在本地存储中的操作对象,该部分正在工作,我只是无法将其发送到console.log(操作)的htmlwhat代码>登录?它正在记录存储在本地存储中的操作对象,该部分正在工作,我只是无法将其发送到html@kjamp它之所以有效,是因为通过action
变量,您希望用数据绑定html。而且const
范围也在该功能范围内only@kjamp它之所以有效,是因为通过action
变量,您希望用数据绑定html。而且const
范围仅在该功能内