2017-06-15 86 views
1

我的聊天页面有小问题:)想要做一些聊天应用程序 我有登录工作,但当我点击按钮聊天它崩溃。我是初学者,所以也许我看不到正确的。这是在离子3聊天页面socket.io离子错误

Home.ts

import { Component } from '@angular/core'; 
import { NavController, IonicPage } from 'ionic-angular'; 
import { Auth } from '@ionic/cloud-angular'; 
import { User } from '@ionic/cloud-angular'; 
import { LoginPage } from '../login/login'; 
import { About } from '../about/about'; 
import { ChatPage } from '../chat/chat'; 

@IonicPage() 
@Component({ 
    selector: 'page-home', 
    templateUrl: 'home.html' 
}) 
export class HomePage { 
    constructor(public navCtrl: NavController, public auth: Auth, public user: User) { 
    console.log(user); 
    } 

    logout() { 
     this.auth.logout(); 
     this.navCtrl.setRoot(LoginPage); 
    } 

    about(){ 
     this.navCtrl.setRoot(About); 
     this.navCtrl.push(About); 
    } 

    chat(){ 
     this.navCtrl.setRoot(ChatPage); 
     this.navCtrl.push(ChatPage); 
    } 
} 

home.html的

<ion-header> 
    <ion-navbar> 
    <ion-title> 
     Home 
    </ion-title> 
    <ion-buttons end> 
     <button ion-button icon-only (click)="logout()"> 
     <ion-icon name="exit"></ion-icon> 
     </button> 
    </ion-buttons> 
    </ion-navbar> 
</ion-header> 

<ion-content padding> 
    <h2>Welcome back, {{ user.details.name }}</h2> 
    <p> 
    <button ion-button color="secondary" block>Something</button> 
    </p> 
    <p> 
    <button ion-button color="secondary" block (click)="chatPage()">Chat</button> 
    </p> 
    <p> 
    <button ion-button color="secondary" block (click)="about()">About</button> 
    </p> 
</ion-content> 

chat.ts

import { Component, NgZone, ViewChild } from '@angular/core'; 
import { NavController, Content } from 'ionic-angular'; 
import { Validators, FormBuilder } from '@angular/forms'; 
import * as io from 'socket.io-client'; 

import { HomePage } from '../home/home'; 

@Component({ 
    selector: 'page-chat', 
    templateUrl: 'chat.html' 
}) 

export class ChatPage { 
    public chatForm: any; 
    @ViewChild(Content) content: Content; 
    messages:any = []; 
    socketHost: string = "http://localhost:3000/"; 
    socket:any; 
    chat:any; 
    username:string; 
    zone:any; 

    chatField: any; 

    constructor(public navCtrl: NavController, private fb: FormBuilder){ 
    this.socket = io.content(this.socketHost); 
    this.zone = new NgZone({enableLongStackTrace: false}); 
    this.socket.on("chat me", (msg) =>{ 
    this.zone.run (() =>{ 
     this.messages.push(msg); 
     this.content.scrollToBottom(); 
    }); 
    }); 
    this.chatForm = fb.group({ 
     'chatField': ['', Validators.compose([Validators.required])], 
     }); 

    this.chatField = this.chatForm.controls['chatField']; 
    } 

    chatSend(v) { 
    let data = { 
     message: v.chatText, 
     username: this.username 
    } 
    this.socket.emit('new message', data); 
    this.chat = ""; 
    } 
    onSubmit(value: string): void { 
     if(this.chatForm.valid) { 

    } 
    } 
} 

chat.html

<ion-header> 
    <ion-navbar> 
    <button ion-button menuToggle> 
     <ion-icon name="menu"></ion-icon> 
    </button> 
    <ion-title>Chat</ion-title> 
    </ion-navbar> 
    <ion-input [(ngModel)] = "username" placeholder="Username"> </ion-input> 
</ion-header> 
<ion-content class="chat"> 
    <ion-list> 
    <div *ngFor="let data of messages"> 
     <ion-item *ngIf="username === data.username" style="border-right: solid 5px red;"> 
     <p class="chat-text" item-left> 
      <strong> {{data.username}}: </strong> 
      {{data.message}} 
     </p> 
     <p class="chat-time" item-right> 
      {{data.date | date:'dd/MM/yyyy'}} 

     </p> 
     </ion-item> 

     <ion-item *ngIf="username !== data.username" style="border-right: solid 5px green;"> 
     <p class="chat-text" item-left> 
      <strong> {{data.username}}: </strong> 
      {{data.message}} 
     </p> 
     <p class="chat-time" item-right> 
      {{data.date | date:'dd/MM/yyyy'}} 

     </p> 
     </ion-item> 

     </div> 
    </ion-list> 
</ion-content> 

<ion-footer> 
    <form padding [formGroup]="chatForm" (ngSubmit)="onSubmit(chatForm.value)"> 
    <ion-item> 
     <ion-input [(ngModel)]="chatField" name="chatText" required placeholder="type here....."></ion-input> 
    </ion-item> 
    <br/><br/> 
    <button type="submit" ion-button [disabled]="!chatForm.valid" block>Send</button> 
    </form> 
</ion-footer> 

,我得到的错误是

Runtime Error 
co.chatPage is not a function 
Stack 
TypeError: co.chatPage is not a function 
    at Object.eval [as handleEvent] (ng:///AppModule/HomePage.ngfactory.js:290:24) 
    at handleEvent (http://localhost:8100/build/main.js:12302:138) 
    at callWithDebugContext (http://localhost:8100/build/main.js:13510:42) 
    at Object.debugHandleEvent [as handleEvent] (http://localhost:8100/build/main.js:13098:12) 
    at dispatchEvent (http://localhost:8100/build/main.js:9277:21) 
    at http://localhost:8100/build/main.js:9867:38 
    at HTMLButtonElement.<anonymous> (http://localhost:8100/build/main.js:35897:53) 
    at t.invokeTask (http://localhost:8100/build/polyfills.js:3:12256) 
    at Object.onInvokeTask (http://localhost:8100/build/main.js:4613:37) 
    at t.invokeTask (http://localhost:8100/build/polyfills.js:3:12177) 
    at n.runTask (http://localhost:8100/build/polyfills.js:3:7153) 
    at HTMLButtonElement.invoke (http://localhost:8100/build/polyfills.js:3:13213) 
Ionic Framework: 3.1.1 
Ionic App Scripts: 1.3.7 
Angular Core: 4.0.2 
Angular Compiler CLI: 4.0.2 
Node: 7.2.0 
OS Platform: Windows 10 
Navigator Platform: Win32 
User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) App 

回答

0

的问题是你有叫你的模板和功能(click)="chatPage()"HomePage.tschat()

做:

<button ion-button color="secondary" block (click)="chat()">Chat</button> 

侧面说明:

你不必做既pushsetRoot

setRoot将创建一个新的导航堆栈并将特定页面设置为Root。 push会将页面推送到当前导航堆栈。 根据您的要求使用其中之一。

+0

运行时错误 未捕获的(在承诺):类型错误:__WEBPACK_IMPORTED_MODULE_3_socket_io_client __内容不是函数类型错误:__WEBPACK_IMPORTED_MODULE_3_socket_io_client __内容并不在新ChatPage 我得到现在这个错误 –

+0

看起来与问题无关的功能。它是更多与添加'socket.io'库及其导入有关。你以前的错误掩盖了这个错误,因为它没有早些时候被调用..我建议..创建一个新的问题,你如何安装这个和你的package.json包含什么等等。 –

+0

它已经添加在离子框架和chat.ts你有导入的套接字 –