Angular 一些Rxjs角代码的语法问题
我在研究反应式编程时发现了这段代码,有人能解释一下这一行发生了什么,语法是什么意思吗Angular 一些Rxjs角代码的语法问题,angular,rxjs,Angular,Rxjs,我在研究反应式编程时发现了这段代码,有人能解释一下这一行发生了什么,语法是什么意思吗 const getCars = query => of(cars.filter(car => isCarMatching(car, query))).pipe(delay(3000)); 从这里开始, 完整代码 const { fromEvent, of } = rxjs; const { delay, tap, map, filter, debounceTime, di
const getCars = query => of(cars.filter(car => isCarMatching(car, query))).pipe(delay(3000));
从这里开始,
完整代码
const { fromEvent, of } = rxjs;
const {
delay,
tap,
map,
filter,
debounceTime,
distinctUntilChanged,
switchMap
} = rxjs.operators;
const cars = [
{
id: 1,
brand: 'Ferrari',
model: 'F40'
},
{
id: 2,
brand: 'Ferrari',
model: 'F50'
},
{
id: 3,
brand: 'Ferrari',
model: 'California'
},
{
id: 4,
brand: 'Porsche',
model: '911'
},
{
id: 5,
brand: 'Porsche',
model: 'Panamera'
}
];
const getCarFullName = ({ brand, model }) =>
`${brand.toLowerCase()} ${model.toLowerCase()}`;
const isCarMatching = (car, query) =>
getCarFullName(car).startsWith(query.toLowerCase());
const getCars = query =>
of(cars.filter(car => isCarMatching(car, query))).pipe(delay(3000));
const onCarsLoadSuccess = matchingCars => console.log(matchingCars);
const carSearch$ = fromEvent(carSearch, 'input').pipe(
map(event => event.target.value),
filter(query => query),
debounceTime(1000),
distinctUntilChanged(),
tap(query => console.log(`About to make an API call with query: ${query}`)),
switchMap(getCars)
);
carSearch$.subscribe(onCarsLoadSuccess);
您能否更具体地说明您对该语法的理解?它主要是箭头函数。你也可以在中找到运算符的所有描述,过滤器是array.prototype.filter。我不理解用const分配函数并执行它的部分。那么,这与RxJS无关。什么不清楚?函数表达式可以像任何其他表达式一样分配给名称。谢谢,第一部分正是我要问的,现在清楚了
const getCars = query =>...
//it's
getCars(query)
{
return ...
}
of(...)
is an observable of the inside parenthesis, e.g.
of({name:"name",surname:"surname"}), return an observable you can subscribe
of({name:"name",surname:"surname"}).susbcribe(res=>{
console.log(res) //return {name:"name",surname:"surname"}
})
cars.filter(car => isCarMatching(car, query))
return the objects in array cars that calling the function "isCarMatthing" return true
NOTE: Not use this.isCarMatching because we are not in a component is a constant
pipe(delay(3000)) is an rxjs operators that delay the response 3000 miliseconds