簡體   English   中英

例外:TypeError:未定義不是Ionic 2中的對象(評估“ pipe.constructor”)嗎?

[英]EXCEPTION: TypeError: undefined is not an object (evaluating 'pipe.constructor') in Ionic 2?

我的Ionic 2應用程序在chrome瀏覽器上可以正常工作,它可以提供預期的輸出。 但是,當它在模擬器或瀏覽器上運行時,會出現以下錯誤。

這是我正在使用的代碼:

<label class="time">{{appointment.DateSent | date:"HH"}}:{{ appointment.DateSent| date:"mm"}}</label>
<label class="month">{{appointment.DateSent| date:"MMM"}}</label>
<label class="day">{{appointment.DateSent| date:"dd"}}</label>
<label class="year">{{appointment.DateSent| date:"yyyy"}}</label>

類的實現:

    import {Page, NavController, NavParams} from 'ionic/ionic';
    import { Component, Pipe, PipeTransform, Inject, OnInit} from 'angular2/core';
    import 'rxjs/add/operator/map';
    import { Http, Headers, URLSearchParams } from 'angular2/http';
    import {SERVER_URL, appName, alertAppName} from '../services/config';
    import {AppointmentsDetailPage} from '../appointments-detail/appointments-detail';
    import {MessagesService} from '../services/messages-service';

    var today = new Date();

    @Page({
      templateUrl: 'build/pages/appointments/appointments.html',
      providers: [MessagesService]
    })
    export class AppointmentsPage implements PipeTransform {
      constructor(http: Http, nav: NavController, messagesService:MessagesService, navParams:NavParams) {
            this.http = http;
            this.messagesService = messagesService;
            this.nav = nav;

            this.selectedItem = navParams.get('item');
      }

ngOnInit() {
        this.messagesService.getAppointmentList()
        .subscribe(data => {

          var appointments = new Array();
              for(var i in data){
                var key = data[i];

                appointments.push({
                  MessageID: key.MessageID,
                  CategoryID: key.CategoryID,
                  DateSent: new Date(key.DateSent),
                  Title: key.Title,
                  MessageContent: key.MessageContent,
                  Sender: key.Sender,
                  Recipient: key.Recipient,
                  DateReceived: key.DateReceived,
                  DateRead: key.DateRead,
                  Note_Direction: key.Note_Direction,
                  Viewed: key.Viewed,
                  AppointmentDateTime: key.AppointmentDateTime,
                  MessageAttachments: key.MessageAttachments
                });
              }


      this.appointments = appointments;

所有日期管道在Chrome瀏覽器中都可以正常運行,沒有錯誤,但是在模擬器或設備上給出了以下異常(取自Safari模擬器調試)

在此處輸入圖片說明

編輯:

Index.html

<!DOCTYPE html>
<html dir="ltr" lang="en">

<head>
  <title>Ionic</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">

  <!--<meta http-equiv="Content-Security-Policy" content="img-src 'self' data:; default-src 'self' http://XX.XX.XX.XX:8084/mypp/">-->
  <!--<meta http-equiv="Content-Security-Policy" content="default-src *; script-src 'self' 'unsafe-inline' 'unsafe-eval' *; style-src  'self' 'unsafe-inline' *">-->
  <meta name="format-detection" content="telephone=no">
  <meta name="msapplication-tap-highlight" content="no">

  <link rel="stylesheet" href="build/css/font-awesome.min.css">
  <link ios-href="build/css/app.ios.css" rel="stylesheet">
  <link md-href="build/css/app.md.css" rel="stylesheet">



</head>

<body>
  <ion-app></ion-app>

  <script src="node_modules/es6-shim/es6-shim.min.js"></script>
  <script src="node_modules/systemjs/dist/system-polyfills.js"></script>
  <script src="lib/ngCordova/dist/ng-cordova.js"></script>
  <script src="cordova.js"></script>
  <script src="build/js/app.bundle.js"></script>
</body>

</html>

您應該加載polyfill(Chrome可以在沒有它們的情況下運行Angular):

<script src="node_modules/es6-shim/es6-shim.min.js"></script>
<script src="node_modules/systemjs/dist/system-polyfills.js"></script>

Sasxa的回答幫助我解決了這個問題。 盡管我嘗試指定路徑,但似乎找不到其資源。 以下資源幫助我獲得了答案。 正如Sasxa所指出的,Safari需要Intl polyfill。

  1. https://www.npmjs.com/package/intl
  2. https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Intl#Browser_Compatibility

我嘗試使用NPM來安裝它,而Bower卻沒有用,最后我決定使用CDN位置,因為它對我有用。

<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=Intl.~locale.en"></script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM