小编awm*_*eer的帖子

离子地理定位插件错误:"没有地理定位的提供者"

我正在尝试在我的ionic2 hello world项目中使用geolocation,并按照官方网站上的说明添加离子插件"Geolocation" .

我运行了这两个命令:

$ ionic plugin add cordova-plugin-geolocation
$ npm install --save @ionic-native/geolocation
Run Code Online (Sandbox Code Playgroud)

这是我的家.:

import { Component } from '@angular/core';
import {Geolocation} from '@ionic-native/geolocation'
import { NavController } from 'ionic-angular';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  map:any=null;
  geoInfo:any={
      resp:'',
      data:''
  };

  constructor(
      public navCtrl: NavController,
      private geolocation: Geolocation
  ) {

  }

  test(){
      this.geolocation.getCurrentPosition().then((resp) => {
          this.geoInfo.resp=JSON.stringify(resp);
          // resp.coords.latitude
          // resp.coords.longitude
      }).catch((error) => {
          console.log('Error getting location', error);
          this.geoInfo.resp='Error getting location';
      });

      let …
Run Code Online (Sandbox Code Playgroud)

typescript cordova-plugins ionic2 angular

24
推荐指数
3
解决办法
2万
查看次数

如何将一个Observable转换为BehaviorSubject?

我正在尝试将Observable转换为BehaviorSubject。像这样:

a$ = new Observable()
b$ = BehaviorSubject.create(new BehaviorSubject(123), a$)
// 
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

a$ = new Observable()
b$ = new BehaviorSubject(a$, 123)
// 
Run Code Online (Sandbox Code Playgroud)

和:

a$ = new Observable()
b$ = a$.asBehaviorSubject(123)
// 
Run Code Online (Sandbox Code Playgroud)

和:

a$ = new Observable()
b$ = a$.pipe(
  toBehaviorSubject(123)
)
// 
Run Code Online (Sandbox Code Playgroud)

但是这些都不起作用。现在,我必须像这样实现:

a$ = new Observable()
b$ = new BehaviorSubject(123)
a$.subscribe(b$)
// 
Run Code Online (Sandbox Code Playgroud)

在课堂上,这有点难看:

class Foo() {
  a$ = new Observable() // Actually, a$ is more complicated than this.
  b$ = new BehaviorSubject(123)

  constructor() {
    this.a$.subscribe(this.b$)
  }
}
Run Code Online (Sandbox Code Playgroud)

因此,有没有更简单的方法可以在不使用类构造函数的情况下将Observable转换为BehaviorSubject?


这是我的真实情况: …

javascript observable rxjs typescript behaviorsubject

7
推荐指数
3
解决办法
5569
查看次数

如何检测离子2中离子搜索条的聚焦和模糊事件?

我现在正在使用ionic2来构建应用程序.我想检测搜索栏输入何时获得焦点以及何时模糊,以便我可以根据当前状态隐藏或显示某些组件.(例如,在用户点击此搜索栏时显示一些建议.)

这是我的代码:

<ion-searchbar #searchbar [(ngModel)]="searchInput"></ion-searchbar>
Run Code Online (Sandbox Code Playgroud)

但是,我发现这个组件似乎没有这两个事件.我试图添加这样的事件监听器,但它不起作用:

<ion-searchbar #searchbar [(ngModel)]="searchInput" (focus)="searchBarOnFocus()"></ion-searchbar>
Run Code Online (Sandbox Code Playgroud)

那么我可以通过其他方式实现此功能吗?

提前致谢!


ion-searchbar有两个文档: component document& api document

dom-events typescript ionic2 angular

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

TypeScript 中常规函数和函数表达式之间的不同类型缩小行为

function foo(item: string | null) {
  if (!item) return null
  return () => {
    function regular() {
      return item
    }
    const arrow = () => item
    console.log(regular().toLowerCase()) // Error: Object is possibly 'null'.
    console.log(arrow().toLowerCase()) // Pass
  }
}
Run Code Online (Sandbox Code Playgroud)

游乐场在这里

在函数中foo我写了两个嵌套函数。一种是常规函数,另一种是箭头函数。虽然我期望返回的两个值的类型都应缩小为string,但事实是regular()得到类型string | null

起初我认为这可能是由于函数参数item是可变的。(本期提到)所以我手动将其转换为const,但仍然出现同样的问题:

function foo(item: string | null) {
  const itemCopy = item // Make sure TypeScript know it is immutable
  if (!itemCopy) return null
  return () => …
Run Code Online (Sandbox Code Playgroud)

typescript

5
推荐指数
1
解决办法
98
查看次数