标签: ionic3

ngOnInit 和 Constructor 被调用两次

由于某种原因,我的函数被调用两次,但我似乎不明白为什么。

我在这里看到这个问题

为什么 ngOnInit 调用了两次?

哪个引用了这个github问题

https://github.com/angular/angular/issues/6782

但这似乎表明我正在多个地方导入该文件,但我认为情况并非如此。

据我所知,我正在利用 Ionic 3 的延迟加载功能。

这是一个精简的 github 存储库的链接,您可以在本地运行来查看问题(很抱歉,我无法找出在 plunker 或 codepen 中运行它的最佳方法)

https://github.com/Jordan4jc/ionic-init-example

这个概念是主应用程序首先从商店加载一个令牌,然后验证它,如果它仍然是有效的路由,EventsPage但如果不是,它将路由到LoginPage

在此示例中,我伪造它并假装令牌有效并路由到EventsPage,正如您将在ngOnInit函数中看到的那样(如果我将其移动到构造函数theconsole.log 中,则事件会被调用两次。这将击中我的服务器获取最新数据,所以我真的不想两次调用我的 API。

编辑:这是内容app.component.ts

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import {Storage} from '@ionic/storage';

@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage:any;
  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, storage: Storage) …
Run Code Online (Sandbox Code Playgroud)

angularjs ionic-framework ionic3

4
推荐指数
1
解决办法
1万
查看次数

如何使用 Renderer2 更改 ionic2 中的按钮文本

我不知道如何使用 ElementRef & Renderer2 类更改 ionic2 中的按钮文本。

这是我到目前为止所尝试过的。

@Component({
  selector: 'component',
  templateUrl: `<button #button type="submit" class="form-button" ion-button 
                [disabled]="!form.valid">Change this text</button>`;
})
export class component {
   @ViewChild('button', {read: ElementRef}) 
   private button : ElementRef;

   constructor(private renderer: Renderer2){
   }

   ionViewDidLoad() {
      this.renderer.setProperty(this.button.nativeElement, 'value', 'Cute alligator');
   }
}
Run Code Online (Sandbox Code Playgroud)

这setProperty只会更改作为属性的按钮的值。但我想更改现在位于按钮内部的按钮文本Change this text。

请帮忙。谢谢。

typescript ionic-framework ionic2 ionic3 angular

4
推荐指数
1
解决办法
2387
查看次数

离子按钮激活/激活

我希望将离子按钮的状态设置为活动状态,但这在 API 文档中根本没有记录。截至目前,当您单击按钮时,“激活”类将添加到按钮,然后稍后删除。

ionic-framework ionic3

4
推荐指数
1
解决办法
7349
查看次数

Ionic 应用程序中没有 NavParams 错误的提供程序

我正在将参数从一个组件传输到 app.component.ts 但错误即将到来。错误:没有 NavParams 的提供者。

这是我的app.component.ts:

import { FrontPage } from "./../pages/front/front";
import { Component, ViewChild } from "@angular/core";
import { Nav, Platform, NavController, NavParams } from "ionic-angular";
import { StatusBar } from "@ionic-native/status-bar";
import { SplashScreen } from "@ionic-native/splash-screen";
import { HomePage } from "../pages/home/home";
import { ListPage } from "../pages/list/list";
import { ProductPage } from "../pages/product/product";
import { LoginpagePage } from "../pages/loginpage/loginpage";

@Component({
  templateUrl: "app.html",
})
export class MyApp {
  @ViewChild(Nav) nav: Nav;
  menuclick: boolean = …
Run Code Online (Sandbox Code Playgroud)

ionic-framework ionic3 angular

4
推荐指数
2
解决办法
1万
查看次数

无法从 Ionic 中的 ion-select 获得选定的值

我正在尝试从 ion-select 中获取所选选项 Value
但是当我从 ion-select 中选择任何值时,我得到了Undefined值,因为我是 ionic 的新手,
我无法自己对其进行排序。

HTML:

<ion-item>
   <ion-label>Quantity</ion-label>
   <ion-select [(ngModel)]="number"  
     (ionChange)="onChange(carbrand)" >
      <ion-option *ngFor="let count of quantity" 
       [value]="count" >{{count}}</ion-option>
      </ion-select>
   <!-- <ion-select [(ngModel)]="number">
      <ion-option *ngFor="let count of quantity" 
      value="count"></ion-option>
   </ion-select> -->
</ion-item>
Run Code Online (Sandbox Code Playgroud)

主页.ts :

onChange(SelectedValue){ 
  console.log("Selected Quantity", SelectedValue); 
}
Run Code Online (Sandbox Code Playgroud)

typescript ionic-framework ionic2 ionic3 angular

4
推荐指数
1
解决办法
5121
查看次数

有没有办法避免在 Ionic 3 中的列表更新后视图闪烁?

我是 Ionic 3 和 firebase 的新手,我正在尝试做一个聊天功能。我的问题是当我用新值更新数组时,视图中的整个列表似乎闪烁/闪烁。有没有办法防止这种情况?我需要更改我的代码吗?

代码(用户聊天.ts)

getChats(){  
//Add by index idea to avoid blinking?
this.chatObserver = this.db.list('chat', ref => ref.child(this.authKey).orderByChild('timestamp'))
  .snapshotChanges().subscribe( snapshot => {
    let reversedSnap = snapshot.slice().reverse();
    let chatArray = [];
    reversedSnap.forEach( element =>{
      let data = element.payload.val();
      data['id'] = element.key;

      this.db.list('profile', ref=> ref.child(data['receiver'])).query.once('value', profileSnap => {
          let profileData = profileSnap.val();
          data['firstName'] = profileData.firstName;
          data['lastName'] = profileData.lastName;
          data['userImage'] = profileData.photos[0];
          data['userKey'] = profileSnap.key;
      })
      this.db.list('messages', ref=> ref.child(this.authKey).child(data['id']).orderByChild('timestamp').limitToLast(1))
        .query.once('value', messageSnap =>{
          messageSnap.forEach( element =>{  //only returns one, foreach to …
Run Code Online (Sandbox Code Playgroud)

firebase ionic2 ionic3 angular

4
推荐指数
2
解决办法
2476
查看次数

NgModule 'AppModule' 的提供者无效

我不明白为什么我一次又一次地出现这个错误......当我有一个新的提供者时。当我有任何其他提供商(如地理定位等)时,出现此错误:

错误:未捕获的错误:NgModule 'AppModule' 的提供程序无效 - 仅允许提供程序和类型的实例,得到:[StatusBar, SplashScreen,?[object Object]?, ...]

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';
import { ImagePicker } from '@ionic-native/image-picker';
import { Crop } from '@ionic-native/crop';
import { AuthService } from '../pages/core/auth.service';
import { UserService } from '../pages/core/user.service';
import { FollowService } from "../pages/core/follow.service";
import { AngularFireModule } from 'angularfire2';
import { …
Run Code Online (Sandbox Code Playgroud)

ionic-framework ionic3 angular

4
推荐指数
2
解决办法
7409
查看次数

在 TypeScript 中调用泛型类型的构造函数

我有以下一段代码:

  public getProducts<T>(): Observable<T[]> {
    return this.httpClient
      .get<T[]>(this.baseUrl + '/users')
      .pipe(
        map((entities) => {
          return entities.map((entity) => {
            return new T(entity);
          })
        }),
        catchError((err) => Observable.throw(err))
      );
  }
Run Code Online (Sandbox Code Playgroud)

这一行给了我一个错误:return new T(entity);

如何在 TypeScript 中调用泛型类型的构造函数?

typescript ionic-framework ionic3 angular

4
推荐指数
1
解决办法
537
查看次数

当 Ionic 3 中的输入处于活动状态时,显示/隐藏密码按钮不起作用

我正在尝试为 Ionic 3 中的密码字段实现显示/隐藏按钮。我从这里得到了代码帮助

登录.html

  <ion-item>
    <ion-input [type]="passwordType" placeholder="Password" formControlName="password"></ion-input>
    <ion-icon item-end [name]="passwordIcon" class="passwordIcon" (click)='hideShowPassword()'></ion-icon>
  </ion-item>
Run Code Online (Sandbox Code Playgroud)

登录.scss

    .passwordIcon{
        font-size: 1.3em;
        position: absolute;
        right: .1em;
        top: .5em;
        z-index: 2;
     }
Run Code Online (Sandbox Code Playgroud)

登录.ts

passwordType: string = 'password';
passwordIcon: string = 'eye-off';
  hideShowPassword() {
    this.passwordType = this.passwordType === 'text' ? 'password' : 'text';
    this.passwordIcon = this.passwordIcon === 'eye-off' ? 'eye' : 'eye-off';
}
Run Code Online (Sandbox Code Playgroud)

我在 .scss 文件中做了一个修改,并将图标设为绝对,以便它出现在输入字段的顶部而不是侧面。当输入字段未处于活动/选择状态时,此图标单击有效,但如果我正在输入字段中键入,则单击无效/无法识别。请帮忙。

解决方案建议我的领域看起来像这样。 在此处输入图片说明

passwords sass show-hide ionic-framework ionic3

4
推荐指数
1
解决办法
3170
查看次数

在 Ionic 4 中使用查询参数

我正在将一个 Ionic 3 项目转移到 Ionic 4。这是一个网络项目。

扫描二维码,页面打开。网址看起来像:

domain.com/qanda/pwa/home?user=simon&skill=ionic&role=Admin&Table=132
Run Code Online (Sandbox Code Playgroud)

直到 Ionic 3,一切都很好。如果我在 Ionic 4 中添加这些参数并运行,它会打开domain.com/qanda/pwa/ - 一个空白页面,默认情况下甚至不会打开主页。

我在.htaccess旁边保留了文件,index.html以便我可以直接从 url 打开任何项目页面。下面是代码.htaccess:

RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

RewriteRule (.*) https://%{HTTP_HOST}/qanda/pwa/index.html
Run Code Online (Sandbox Code Playgroud)

如何在 Ionic 4 中实现查询参数?

ionic-framework ionic3 angular ionic4

4
推荐指数
2
解决办法
6357
查看次数