【问题标题】:sharing data between components and pages in ionic在 ionic 中的组件和页面之间共享数据
【发布时间】:2019-07-31 18:44:50
【问题描述】:

您好,我正在学习 ionic,我想将组件文件访问到主页,当我在主页中提到组件类链接时,只有第一页显示剩余页面没有进入页面。

如何解决这个问题或告诉我任何其他方法来解决这个问题?


组件模块.ts

import { NgModule } from '@angular/core';
import { ImageComponent } from './image/image';
import { IonicModule } from 'ionic-angular';
import { VideoComponent } from './video/video';
import { AudioComponent } from './audio/audio';
import { TextComponent } from './text/text';

@NgModule({
    declarations: [
        ImageComponent,
        VideoComponent,
        AudioComponent,
        TextComponent
    ],
    imports: [
        IonicModule
    ],
    exports: [
        ImageComponent,
        VideoComponent,
        AudioComponent,
        TextComponent
    ]
})
export class ComponentsModule { }

appmodule.ts

@NgModule({
  declarations: [
    MyApp,
    HomePage
  ],
  imports: [
    BrowserModule,
    ComponentsModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    HomePage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    ActionSheet 

  ]
})

app.html

<ion-header>
  <ion-navbar>
    <ion-title>
      Ionic Blank
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>

  <ion-card>
    <ion-card-header>
      Image
    </ion-card-header>
    <ion-card-content>
      <image></image>
    </ion-card-content>
  </ion-card>


  <ion-card>
    <ion-card-header>
      video
    </ion-card-header>
    <ion-card-content>
      <video></video>
    </ion-card-content>
  </ion-card>

  <ion-card>
      <ion-card-header>
        Audio
      </ion-card-header>
        <ion-card-content>
        <audio></audio>
      </ion-card-content>
    </ion-card>

</ion-content>

【问题讨论】:

  • 可以用 NavController 传递数据,教程如下:hackernoon.com/…
  • 我的代码在组件类中,所以我想通过页面访问这些组件文件。我知道在页面之间共享数据,但这让我很困扰
  • 您究竟想访问哪些组件文件?变量?你是在讨论 .ts 文件还是 .html 文件?
  • 感谢组件 html 页面中的响应,我在 div 标签中提到了按钮,它已解决。
  • @johnny 很高兴你修复了它。顺便说一句,您从未将 component.html 页面代码放在您的问题中

标签: angular ionic3


【解决方案1】:

我知道有两种方法可以实现通过组件传递数据。

1) 使用服务(我更喜欢这个)

data.service.ts

    import { Injectable } from '@angular/core';

    @Injectable()
    export class dataService {
        data: string;

        setData(data) {
            this.data = data;
        }

        getData(){
           return this.data;
        }
    }

app.component.ts

import { Component } from '@angular/core';
import { dataService } from './server.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  constructor(private dataService: dataService){}

  getData() {
      retrun this.dataService.getData();
  }

}

2) 使用导航控制器

 //When you are using the push to switch pages you pass the data to the otherpage
    this.navCtrl.push(HomePage, {
          data: user
    });

在您刚刚访问的页面(在本示例中为主页)中,您可以像这样访问数据:

constructor(public navCtrl: NavController, public navParams: NavParams) {
    this.user = navParams.get('data');
  }

【讨论】:

  • 谢谢先生,很好的信息,但是如何将数据服务的值显示到component.html?
  • 您必须首先访问组件中的服务,然后在 UI 中调用您已经添加到组件 ts 文件中的服务所需的任何内容
猜你喜欢
  • 2023-03-26
  • 2019-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-09
  • 1970-01-01
  • 2019-12-20
相关资源
最近更新 更多