Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

angular - ngrx store selector failing on app import from custom library

I have an angular library with store implementation and this library is packaged as a NPM and used in different angular application.

I'm trying to use a ngrx store selector which was exported in the library in my different angular project and the application throws an error.

Module not found: Error: Can't resolve '@lib/core-lib/lib/core-store/store/account/account.selector' in 'C:workspaceClientAngularAppsrcappapp.component'

Angular Library Implementation:

import { createSelector } from '@ngrx/store';
import { ILibState } from '../../lib.state';
import { IAccountState } from './account.state';

const selectAccounts = (state: IAppState) => state.accounts;

export const selectSelectedAccount = createSelector(
  selectAccounts,
  (state: IAccountState) => state?.selectedAccount
);

exported this selector from the public_api.ts

export * from './lib/core-store/store/account/account.selector'
// Also tried this
// export { selectSelectedAccount } from './lib/core-store/store/account/account.selector'
// Also tried the barrel approach
// export * from './lib/core-store/store/account/index'

AngularApp Usage

app.component.ts

import { ILibState } from '@lib/core-lib/lib/core-store/lib.state';
import { select, Store } from '@ngrx/store';
import { takeUntil } from 'rxjs/operators';
import { selectSelectedAccount } from '@lib/core-lib/lib/core-store/store/account/account.selector';

this._store
  .pipe(select(selectSelectedAccount), takeUntil(this.destroy$))
  .subscribe((res) => {
    if (res && this.selectedAccountCode !== res) {
      this.selectedAccountCode = res.account;
    }
  });

Angular Library works fine without any issue and getting this piece of selector into my angular application is giving me error on compilation.

I tried to provide as much detail as possible and let me know if I need to add anything else.

Any answer on this is much appreciated

question from:https://stackoverflow.com/questions/65903604/ngrx-store-selector-failing-on-app-import-from-custom-library

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Without some background on your folder structure and configuration it's going to be hard but let's stick to the error

Module not found: Error: Can't resolve '@lib/core-lib/lib/core-store/store/account/account.selector' in 'C:workspaceClientAngularAppsrcappapp.component'

This means that it can not find the file @lib/core-lib/lib/core-store/store/account/account.selector.ts which you intend to import

I suggest you first check the path is correct.

Besides that, there are a couple things I think you should double check:

  • Why are you using the alias @lib if you said this library is in an npm package?
  • Why are you doing deep imports if you exported those functionalities from public_api.ts?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...