Angular Cordova - >如果路由到android cordova

时间:2017-12-19 20:49:27

标签: android angular cordova http angular5

我正在尝试将本地json文件加载到我的服务组件中以使用它填充模板。 http呼叫始终在我的浏览器中工作,但与cordova捆绑在一起,只有在我直接使用标签而不是路由模块时才能使用。 :S

在我的导航组件中

ngOnInit() {
    alert('enter on init')
    this.chatService.getJSON().subscribe(data => {
       this.Questionaire = data;
       console.log('fill Questionaire');
       console.log(this.Questionaire.UserChatContent);
       this.createNewItem(this.Questionaire);
    }, err => {
       console.log('error');
       err.forEach(e => alert(e) );
    });
       console.log('leave on init');
}

这是我的路由模块

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, Routes } from '@angular/router';
import {ChatViewComponent} from './chat-view/chat-view.component';
import {DailyTrainingComponent} from './daily-training/daily-training.component';
import {HttpClient, HttpClientModule} from '@angular/common/http';

const routes: Routes = [
  { path: 'chat', component: ChatViewComponent },
  { path: 'training', component: DailyTrainingComponent }
];

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forRoot(routes)
  ],
  exports: [ RouterModule ]
})

export class AppRoutingModule { }

现在问题:

//the following code works fine in cordova on android
//and in my browser window (firefox and chrome)
<div class="fullScreen">
  <app-chat-view></app-chat-view>
</div>

// works in browser like its supposed to 
// but in cordova on android the http call in the ngOnInit() in the component always fails :S 
<div class="fullScreen">
  <nav id="navStyle">
    <a class="navElem" routerLink="/fd" (click)="navActive(0)"><div class="navOne"></div></a>
    <a class="navElem" routerLink="/training" (click)="navActive(1)"><div class="navTwo"></div></a>
    <a class="navElem" routerLink="/chat" (click)="navActive(2)"><div class="navChat"></div></a>
    <a class="navElem" routerLink="/df" (click)="navActive(3)"><div class="navFour"></div></a>
    <a class="navElem" routerLink="/d" (click)="navActive(4)"><div class="navFive"></div></a>
  </nav>
  <router-outlet></router-outlet>
</div>

我是角色5的新手,所以我真的希望这不是一个愚蠢的问题。 :S 提前谢谢:D

(这里我使用的是HttpClient和HttpClientModule服务)

import { Injectable } from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {HttpClient, HttpClientModule} from '@angular/common/http';

@Injectable()
export class ChatService {
  Questionaire: object;

  constructor(private http: HttpClient) {
        this.getJSON().subscribe(data => {
        alert('Object: ' + data.UserChatContent);
        this.Questionaire = data;
      });
  }

  public getJSON(): Observable<any> {
    return this.http.get('./assets/questionaire.json');
  }

}

0 个答案:

没有答案