在 Angular 6 中使用以下方法生成新服务时:
ng generate service <service-name>
Run Code Online (Sandbox Code Playgroud)
angular.json
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"test-ang": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist",
"index": "src/index.html",
"main": "src/main.ts",
"tsConfig": "src/tsconfig.app.json",
"polyfills": "src/polyfills.ts",
"assets": [
"src/assets",
"src/favicon.ico"
],
"styles": [
"src/styles.css"
],
"scripts": []
},
"configurations": {
"production": {
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"fileReplacements": [ …Run Code Online (Sandbox Code Playgroud) 当尝试调用_.debounce(fn, wait);apollo-client 调用useLazyQuery(...)时,它会第一次对查询进行反跳,然后调用查询函数,但此后它会在每次更改时继续调用查询,而不会发生任何反跳。
但是,如果我使用 aconsole.log(...)而不是调用,useLazyQuery(...)它将完美地工作。
第一次工作,但随后立即调用该函数,没有任何去抖:
const [value, setValue] = useState('');
const [search, { loading, data }] = useLazyQuery(SEARCH_QUERY, { variables: { searchString: value } });
const debouncer = React.useCallback(_.debounce(search, 1500), []);
...
<call to debouncer() with onChange event>
Run Code Online (Sandbox Code Playgroud)
每次都完美运行:
const [value, setValue] = useState('');
const [search, { loading, data }] = useLazyQuery(SEARCH_QUERY, { variables: { searchString: value } });
const debouncer = React.useCallback(_.debounce(val => (console.log(val)), 1500), []);
...
<call to debouncer() …Run Code Online (Sandbox Code Playgroud)