在使用ng g s serviceName
创建新服务时,我收到 error Expected 0参数,但得到1。
这是我的 package.json
{
"name": "rapid",
"version": "1.1.9",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build --prod",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "^5.2.0",
"@angular/common": "^5.2.0",
"@angular/compiler": "^5.2.0",
"@angular/core": "^5.2.11",
"@angular/forms": "^5.2.0",
"@angular/http": "^5.2.0",
"@angular/platform-browser": "^5.2.0",
"@angular/platform-browser-dynamic": "^5.2.0",
"@angular/router": "^5.2.0",
"@asymmetrik/ngx-leaflet": "^4.0.0",
"@dvsl/zoomcharts": "^1.18.4",
"@ng-bootstrap/ng-bootstrap": "^4.0.3",
"@types/c3": "^0.6.0",
"@types/mixpanel": "^2.14.1",
"angular-2-dropdown-multiselect": "^1.8.1",
"angular-confirmation-popover": "^4.2.0",
"angular-datatables": "^6.0.0",
"angular2-tag-input": "^1.2.3",
"angular2-toaster": "^6.1.0",
"aws-sdk": "^2.271.1",
"bootstrap": "^3.3.7",
"bootstrap-daterangepicker": "^3.0.3",
"c3": "^0.6.6",
"core-js": "^2.4.1",
"crypto-js": "^3.1.9-1",
"datatables.net": "^1.10.18",
"datatables.net-dt": "^1.10.18",
"file-saver": "^2.0.0",
"jquery": "^3.3.1",
"leaflet": "^1.3.4",
"mixpanel-browser": "^2.28.0",
"moment": "^2.22.2",
"ng-multiselect-dropdown": "^0.1.9",
"ng-recaptcha": "^4.2.1",
"ng2-table": "^1.3.2",
"ng2-tooltip-directive": "^2.1.6",
"ngx-bootstrap": "^3.1.2",
"ngx-cookie-service": "^1.0.10",
"ngx-popover": "0.0.16",
"ngx-tooltip": "0.0.9",
"ngx-webstorage": "^2.0.1",
"popper.js": "^1.14.3",
"rxjs": "^5.5.6",
"tslib": "^1.9.0",
"xlsx": "^0.14.1",
"zone.js": "^0.8.19"
},
"devDependencies": {
"@angular-devkit/build-angular": "^0.12.4",
"@angular/cli": "^7.2.1",
"@angular/compiler-cli": "^5.2.0",
"@angular/language-service": "^5.2.0",
"@types/datatables.net": "^1.10.11",
"@types/jasmine": "~2.8.3",
"@types/jasminewd2": "~2.0.2",
"@types/jquery": "^3.3.4",
"@types/node": "~6.0.60",
"codelyzer": "^4.0.1",
"jasmine-core": "~2.8.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "^3.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "^5.4.0",
"ts-node": "~4.1.0",
"tslint": "~5.9.1",
"typescript": "~2.5.3"
}
}
通过从@Injectable中取出providerIn参数,该服务将以相同的方式工作,或者我需要修改我的代码以解决此错误,我通过ng g的serviceName创建了服务以生成service.ts文件
答案 0 :(得分:0)
这是Angular 6中的新功能,
我认为您需要全局更新您的angular-cli。 请使用以下命令。 npm install -g @ angular / cli npm install @ angular / cli
更新package.json ng update @ angular / cli
更新@ Angular / Core ng update @ angular / core
答案 1 :(得分:0)
正如其他人所提到的,providerIn语法是从Angular 6开始的新语法。您正在使用Angular 5,因此该方法将无效。 在Angular 5中,您需要在模块之一中列出服务。如果您需要单例服务(例如将使用angular 6语法创建的服务),则需要在根模块中提供该服务。这很可能是app.module(如果您使用angular cli来创建应用程序。另请参见:https://angular.io/guide/singleton-services。 如果不需要单例服务,我建议您在使用它的模块中提供服务。 不管您决定在哪里提供服务,它都应该看起来像这样:
@NgModule({
imports: [.. some imports...],
providers: [MixPanelService],
declarations: [..some components from your module...],
exports: [...some components that need to be available outside of your module...],
})
export class MyModule{}
答案 2 :(得分:0)
我们可以删除@Injectable()参数,如下所示,一切正常
@Injectable()
export class MixpanelService {
constructor() { }
}