ionic V3.3開發踩坑集錦

GitHub地址:github.com/JerryMissTo… ,歡迎關注css

最近在使用ionic作項目,遇到過一些問題,現記錄下來,說不定有緣人會須要它。咱們的App進入就是登陸界面,登錄成功後會進入到底部有若干Tab的主界面。
Platform Version:html

  • ionic:3.3.0
  • Cordova:7.0.1

最近更新了一篇V3.10的開發,地址是:ionic V3.10 開發踩坑集錦,假如兩者有衝突,以最新的爲準。android

打包出來的apk啓動緩慢,有白屏

使用 ionic cordova build android --prod --release 打包文件會更小,啓動更迅速,親測有效ios

打包出來的APK沒有權限

使用Android Studio打開生成的工程,在Manifest.xml中設置權限,在個人Mac上遇到沒法進行網絡訪問的狀況,只得另外添加,可是在另外一同事那兒沒有這個問題,打包直接就能夠進行網絡訪問,真是奇怪git

從登陸界面進入主頁,按返回鍵能夠返回至登陸頁

在登陸成功的處理代碼段中添加this.navCtrl.setRoot(TabsPage),其中TabsPage是主界面。一般App打開就是登陸界面,登錄後纔是主界面。咱們在app.component.ts中設置了root=LoginPage,因此一進去就是登陸界面,在主界面點擊返回按鈕會退到登陸界面,執行上述代碼後從新設置Root界面就能夠解決。github

添加自定義字體圖標

src目錄下新建icon文件夾,把字體文件放進去。而後在theme/variables.scss中後面添加如下內容,注意把相應位置替換成你本身的:web

$iconfont-path: "../assets/icon";

@font-face {
  font-family: "iconfont";
  src: url('#{$iconfont-path}/iconfont.eot?t=1495679878046'); /* IE9*/
  src: url('#{$iconfont-path}/iconfont.eot?t=1495679878046#iefix') format('embedded-opentype'), /* IE6-IE8 */
  url('#{$iconfont-path}/iconfont.woff?t=1495679878046') format('woff'), /* chrome, firefox */
  url('#{$iconfont-path}/iconfont.ttf?t=1495679878046') format('truetype'), /* chrome, firefox, opera, Safari, Android, iOS 4.2+*/
  url('#{$iconfont-path}/iconfont.svg?t=1495679878046#iconfont') format('svg'); /* iOS 4.1- */
}

.iconfont {
  font-family: "iconfont" !important;
  font-size: 1.6rem;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.ion-md-customer-home:before,
.ion-ios-customer-home:before,
.ion-ios-customer-home-outline:before,
.ion-md-customer-rank:before,
.ion-ios-customer-rank:before,
.ion-ios-customer-rank-outline:before,
.ion-md-customer-stock:before,
.ion-ios-customer-stock:before,
.ion-ios-customer-stock-outline:before {
  @extend .iconfont;
}

.ion-md-customer-home:before { //在這裏自定義你的名字,例如:customer-home,前綴也要加上,與平臺相關
  content: "\e60f";
}

.ion-ios-customer-home:before {  //選中
  content: "\e611";
}

.ion-ios-customer-home-outline:before {   //未選中
  content: "\e60f";
}

.ion-md-customer-rank:before {
  content: "\e60d";
}

.ion-ios-customer-rank:before {
  content: "\e60e";
}

.ion-ios-customer-rank-outline:before {
  content: "\e60d";
}

.ion-md-customer-stock:before {
  content: "\e610";
}

.ion-ios-customer-stock:before {
  content: "\e612";
}

.ion-ios-customer-stock-outline:before {
  content: "\e610";
}
$tabs-ios-tab-text-color-active:#f6670B; //設置點擊後的顏色
$tabs-ios-tab-icon-color-active:#f6670B;
$tabs-md-tab-text-color-active:#f6670B;
$tabs-md-tab-icon-color-active:#f6670B;複製代碼

而後在須要的地方,例如在tabs.html中:chrome

<ion-tabs>
    <ion-tab [root]="tab1Root" tabTitle="{{ 'TABS.TAB1_TITLE' | translate }}" tabIcon="customer-home"></ion-tab>
    <ion-tab [root]="tab2Root" tabTitle="{{ 'TABS.TAB2_TITLE' | translate }}" tabIcon="customer-rank"></ion-tab>
</ion-tabs>複製代碼

最後,在app.scss中添加如下代碼,防止圖標偏上:npm

.tabs-ios .tab-button-icon::before {
  vertical-align: middle;
}複製代碼

ios平臺上,界面底色可能會變黑色

使用<ion-content></ion-content>包裹內容api

國際化

使用官方推薦的ngx-translate,而不是ng2-translate。地址是:ionicframework.com/docs/develo… ionic V3是基於Angular2的,因此國際化也是沿用其方法,地址爲:github.com/ngx-transla…

Token過時返回至登陸頁

檢測到Token過時,應該跳轉到登陸頁,從新登陸,具體方法是:在須要跳轉的地方,執行如下代碼

this.navCtrl.setRoot(LoginComponent);複製代碼

此時可能會出現底下的Tab顯示的問題,隱藏它:

ngOnInit() {
    let elements = document.querySelectorAll(".tabbar");
    if (elements != null) {
      Object.keys(elements).map((key) => {
        elements[key].style.display = 'none';
      });
    }
  }複製代碼

從含Tab的主界面進入二級界面後Tab仍然顯示

在二級界面中加入以下代碼:

//進入隱藏Tab
ngOnInit() {
    let elements = document.querySelectorAll(".tabbar");
    if (elements != null) {
      Object.keys(elements).map((key) => {
        elements[key].style.display = 'none';
      });
    }
  }

//返回至主界面顯示Tab
ngOnDestroy() {
    let elements = document.querySelectorAll(".tabbar");
    if (elements != null) {
      Object.keys(elements).map((key) => {
        elements[key].style.display = 'flex';
      });
    }
  }複製代碼

ionic V3使用ECharts V3.6

cd /項目的根目錄下
npm install echarts --save複製代碼

而後在使用的.ts文件中導入:

import echarts from 'echarts';複製代碼

事件通知

ionic V3.X 自己提供了發佈-訂閱風格的應用級別的事件系統Events ,使用起來簡單方便,具體用法見:ionicframework.com/docs/api/ut…

相關文章
相關標籤/搜索