【问题标题】:Ionic App.component.html Session not showingIonic App.component.html 会话未显示
【发布时间】:2020-04-13 06:37:00
【问题描述】:

我想在 app.component.html 中显示名为 {{nama_cust}} 的 html 会话,但它没有显示任何内容/空白,代码有问题吗?还是 app.component 无法处理会话? 这是我的代码。

app.component.html

 <ion-app>
  <ion-split-pane>
    <ion-menu type="overlay" id="menu-avatar">
      <ion-header>
        <ion-toolbar>
          <ion-title>{{nama_cust}}</ion-title>
        </ion-toolbar>
      </ion-header>
      <ion-content>
        <ion-list>
            <div #header>
                <ion-row style="align-items:center;">
                  <ion-col col-6>
                    <img class="user-avatar" (click)="asd()" src="assets/ava/dum.jpg"/>
                  </ion-col>
                  <ion-col col-3>
                    <h4 id="tes"> {{nama_cust}} </h4>
                  </ion-col>
                </ion-row>
              </div>

app.component.ts

import { Component, NgModule, OnInit } from '@angular/core';
import { Platform, ToastController, IonicModule } from '@ionic/angular';
import { Router } from '@angular/router';
import { Storage, IonicStorageModule } from '@ionic/storage';
import { PostProvider } from 'src/providers/post-provider';
import { DomSanitizer } from '@angular/platform-browser';

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss']
})


export class AppComponent implements OnInit {
  public appPages = [
    {
      title: 'Home',
      url: '/home',
      icon: 'home'
    },
    {
      title: 'List',
      url: '/list',
      icon: 'list'
    }
  ];

  anggota: any;
  nama_cust: string;

  constructor(
   private router: Router,
   private postPvdr: PostProvider,
   private storage: Storage,
   public toastCtrl: ToastController,
   private sanitizer: DomSanitizer
  ) {}

  ngOnInit() {
  }

  ionViewWillEnter() {
    this.storage.get('session_storage').then((res) => {
      this.anggota = res;
      this.nama_cust = sessionStorage.getItem(this.nama_cust);
      console.log(res);
    });
  }
}

console.log(res) 正在显示会话的内容,但是当我在 html 中将会话称为 {{nama_cust}} 时,它什么也没显示。 Here's how it looks,猫图上面和旁边应该有文字,但是什么都没有显示。

【问题讨论】:

    标签: android html angular ionic-framework ionic4


    【解决方案1】:

    您正在使用undefined 键在sessionStorage 上调用getItem()

    this.nama_cust = sessionStorage.getItem(this.nama_cust);
    

    所以没有什么可以检索的,这就是你什么都看不到的原因。 nama_cust 已初始化,但未分配值。

    你需要从响应中得到它:

    this.storage.get('session_storage').then((res) => {
      this.anggota = res;
      this.nama_cust = res.result.nama_cust;
      console.log(res);
    });
    

    【讨论】:

    • 谢谢,它现在可以工作了,但它只有在我刷新后才会出现。有什么建议吗?
    • 插值时使用异步管道。 {{nama_cust |异步}}
    猜你喜欢
    • 2018-02-20
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2021-03-22
    相关资源
    最近更新 更多