我有一个Orders数组,想要创建一个可观察值,它给出计算出的价格总和。我正在使用Angular 6。
import { map, reduce, isEmpty } from 'rxjs/operators';
import { Observable } from 'rxjs';
list$: Observable<IOrder[]>;
total$: Observable<number>;
empty$: Observable<boolean>;
ngOnInit() {
this.list$ = this.service.asList.asObservable(); // asList is a BehaviorSubject
this.empty$ = this.list$.pipe(isEmpty());
this.total$ = this.list$.pipe(
map(order => order.price), // <-- Property 'price' does not exist on type 'IOrder[]'
reduce((total, price) => total + price, 0)
);
}
Run Code Online (Sandbox Code Playgroud)
地图运算符中的“订单”是Order []而不是Order。我究竟做错了什么 ?
我有一个异步生成器函数(批处理作业),随着时间的推移它变得相当大。我想把它分成多个功能:
async *execute(): AsyncGenerator<ISyncState> {
await doThis();
await doThis2();
await doThis3();
yield "this";
await doThis4();
await doThis5();
await doThat();
yield "that";
// .. many more functions
}
async doThis() {...}
async doThat() {...}
async doThis2() {...}
Run Code Online (Sandbox Code Playgroud)
来电者:
const gen = execute();
for await (const syncState of gen)
// do something
Run Code Online (Sandbox Code Playgroud)
我想把它变成:
async *execute(): AsyncGenerator<ISyncState> {
await step1();
await step2();
await step3();
}
async step1() {
await doThis();
yield "this1"; <-- doesn't work
await doThis2();
yield "this2"; <-- doesn't work
await …Run Code Online (Sandbox Code Playgroud) 此命令从命令行成功运行:
// /home/[user]/testproject/target/dependency/testproj-1.0-SNAPSHOT.jar
~/testproject/target/dependency$ jar -xf *.jar
Run Code Online (Sandbox Code Playgroud)
但是当我尝试通过 ProcessBuilder 运行相同的命令时,我得到:
java.io.FileNotFoundException: *.jar (No such file or directory)
Run Code Online (Sandbox Code Playgroud)
流程构建器代码:
ProcessBuilder builder = new ProcessBuilder("jar", "-xf", "*.jar");
builder.directory("/home/[user]/testproject/target/dependency");
Process process = builder.start();
int exitValue = process.waitFor();
Run Code Online (Sandbox Code Playgroud)
如何使其与命令参数中的“*”一起使用?
我有以下问题:
主要活动:
<android.support.design.widget.CoordinatorLayout
.......>
<android.support.design.widget.AppBarLayout
......>
<android.support.v7.widget.Toolbar
app:layout_scrollFlags="scroll|enterAlways"
....../>
</android.support.design.widget.AppBarLayout>
<FrameLayout
app:layout_behavior="@string/appbar_scrolling_view_behavior"
....../>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
当FrameLayout包含带有recyclerview的片段时,向上滑动recyclerview确实会向上滚动工具栏。
问题在于以下情况:
此代码执行片段更改:
fragment = .. get new fragment ..
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame, fragment);
fragmentTransaction.commit();
Run Code Online (Sandbox Code Playgroud)
添加此代码无济于事:
getSupportActionBar().show();
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
toolbar.translationY(0).start();
Run Code Online (Sandbox Code Playgroud)
但没有成功。
如何以编程方式使向上滚动(不可见)工具栏再次向下滚动?
android material-design android-5.0-lollipop android-toolbar
在Java中我有这两个类:
public class Base {
protected Long id;
// getter setter
}
public User extends Base {
private String name;
private Integer age;
public User(Long id) { this.id = id; }
// getter setter
}
Run Code Online (Sandbox Code Playgroud)
我在Kotlin创建了这两个类:
open class Base(var id: Long? = null)
class User(
var id: Long? = null,
var name: String? = null,
var age: Int? = null
) : Base()
Run Code Online (Sandbox Code Playgroud)
现在在Java中我想用只有'id'参数调用User()构造函数:
new User(5);
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎不对,因为通过这样做,我重新声明了User类中的"id"字段.
如何在Kotlin中设置基类的id字段(就像我用Java做的那样"this.id = id;"?
我正在使用ngx-progressbar,它与从服务,组件或解析器中启动的http请求一起使用时效果很好。
请注意,在http请求期间不需要手动触发进度条(通过服务等)。它是自动触发的。
不幸的是,当从NGXS状态内发出http请求时,它无法按预期方式工作:
我为每种情况创建了一个按钮:
这并没有工作,没有出现进度条(或出现,但挂起,未完成100%)
@Action(LoadUrl)
load({ setState }: StateContext<string>) {
return this.http.get<any>('https://jsonplaceholder.typicode.com/posts/1').pipe(tap(res => console.log(res)));
}
Run Code Online (Sandbox Code Playgroud)
这确实按预期工作,出现进度条
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
// ...
makeRequestComponent() {
this.http.get<any>('https://jsonplaceholder.typicode.com/posts/1').pipe(tap(res => console.log(res))).subscribe();
}
}
Run Code Online (Sandbox Code Playgroud)
我问了ngx-progressbar的开发人员,他实际上找到了问题并通过将http调用包装在区域中来解决了。这确实有效,出现进度条:
@State<string>({
name: 'testState',
defaults: ''
})
export class TestUrlState {
constructor(private http: HttpClient, private zone: NgZone) { }
@Action(LoadUrl)
load({ setState }: StateContext<string>) {
this.zone.run(() => {
this.http.get<any>('https://reqres.in/api/users?delay=2').pipe(
tap(res => console.log(res))
).subscribe();
});
} …Run Code Online (Sandbox Code Playgroud) 我有这个结构:
var entities = {
1: {'name': 'Fred', 'age': 35},
2: {'name': 'Hans', 'age': 47},
3: {'name': 'Bert', 'age': 27}
}
Run Code Online (Sandbox Code Playgroud)
我需要这样的东西:
var ids = entities.filter( p => p.age > 30);
Run Code Online (Sandbox Code Playgroud)
哪个应该返回一个数组:
[1, 2]
Run Code Online (Sandbox Code Playgroud)
有没有方便的方法来做到这一点?例如Lodash等?
angular ×2
java ×2
javascript ×2
android ×1
asynchronous ×1
ecmascript-6 ×1
generator ×1
kotlin ×1
lodash ×1
ngxs ×1
rxjs ×1
typescript ×1