我有一個離子應用程序和角度web應用程序,它們都使用相同的REST API。對於最初的開發,我已經在兩個項目中複製了api實現代碼,但是現在我想將api服務解壓縮到鏈接的npm包中,這兩個項目都可以作爲模塊使用和導入。該軟件包編譯良好,但我的Ionic應用程序無法從軟件包中的服務模塊中找到提供程序。離子應用程序沒有找到鏈接包中的提供者
我的服務封裝模塊看起來是這樣的:
程序包的index.ts文件看起來是這樣的:
export { MyAppServicesModule } from './myapp-services.module';
我tsconfig.json文件
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"declaration": true,
"outDir": "out",
"rootDir": "src",
"experimentalDecorators": true
},
"exclude": [
"node_modules",
"out"
]
}
package.json
{
"name": "@myapp/services",
"version": "1.0.0",
"description": "My App services",
"main": "index.js",
"scripts": {
"test": "test"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^8.0.19",
"typescript": "^2.4.2"
},
"dependencies": {
"@angular/core": "^4.3.3",
"@angular/http": "^4.3.3",
"amazon-cognito-identity-js": "^1.19.0",
"rxjs": "^5.4.2"
}
}
編譯包後,我聯繫了包
npm link
在我的應用程序代碼中,我創建一個鏈接到我的包
npm link @myapp/services
我然後導入服務模塊到我主要的應用程序模塊
@NgModule({
declarations: [
MyApp
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp),
MyAppServicesModule,
LoginPageModule,
DocumentsPageModule,
],
bootstrap: [IonicApp],
entryComponents: [
MyApp
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}
而在我的登錄頁面我輸入AuthenticationService
並聲明它的構造函數,因此它得到由依賴注入
import { AuthenticationService } from '@myapp/services'
@IonicPage()
@Component({
selector: 'page-login',
templateUrl: 'login.html',
})
export class LoginPage {
constructor (public navCtrl: NavController,
public navParams: NavParams,
public authenticationService: AuthenticationService) {}
設置,但想爲我的應用程序
typescript: src/pages/login/login.ts, line: 27
Cannot find name 'AuthenticationService'.
L26: public navParams: NavParams,
L27: public authenticationService: AuthenticationService) {}
我也得到了所有引用同樣的錯誤任何時候我得到以下錯誤來自服務模塊的提供者。我嘗試列出服務模塊的exports
部分中的服務,但這並未解決問題。
我離子版本
Ionic Framework: 3.2.1
Ionic App Scripts: 1.3.7
Angular Core: 4.1.0
Angular Compiler CLI: 4.1.0
Node: 7.7.1
OS Platform: macOS Sierra
Navigator Platform: MacIntel
User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4
感謝
當然。這就是我所做的。我的服務列在我的包模塊的提供者部分,但是當應用程序導入包模塊時,爲什麼應用程序不能看到導入模塊的提供者部分中列出的服務? –