Angular Injectable装饰器-预期有0个参数,但有1个 [英] Angular Injectable decorator - Expected 0 arguments but got 1

查看:125
本文介绍了Angular Injectable装饰器-预期有0个参数,但有1个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Angular的基础教程,但遇到一个小的编译问题.

I am just getting started with the base tutorial for Angular but I face a small compilation issue.

我正在关注Angular网站上的教程.我坚持使用依赖项注入部分,因为它不想编译.

I am following the tutorial on Angular's website. I am stuck with the dependency injection part because it doesn't want to compile.

在教程中,他们要求使用以下命令创建服务:

In the tutorial they ask to create a service with :

ng generate service hero

在此生成的服务中,您具有这样的装饰器:

In this generated service you have a decorator like this :

@Injectable()

然后他们要求像这样向装饰器添加参数:

Then they ask to add a parameter to that decorator like this :

@Injectable({
  providedIn: 'root'
})

当我尝试这样做时,我遇到TypeScript错误:

When I try to do so I have a TypeScript error telling me :

错误TS2554:预期参数为0,但得到1. 我使用的是那些版本:

error TS2554: Expected 0 arguments, but got 1. The versions I use are those ones :

Angular CLI: 1.7.4
Node: 9.5.0
OS: win32 x64
Angular: 5.2.10
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

@angular/cli: 1.7.4
@angular-devkit/build-optimizer: 0.3.2
@angular-devkit/core: 0.3.2
@angular-devkit/schematics: 0.3.2
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 1.10.2
@schematics/angular: 0.3.2
@schematics/package-update: 0.3.2
typescript: 2.5.3
webpack: 3.11.0

知道为什么它不接受参数吗?

Any idea why it doesn't accept the parameter ?

提前谢谢!

推荐答案

这是Angular 6中的新功能.

This is a new feature in Angular 6.

@Injectable({
  providedIn: 'root'
})

您有两个选择:

1)请参阅Angular 5文档. (如@ r-richards所述-在左侧导航栏底部的下拉菜单中将文档版本更改为v5.)

1) Refer to the Angular 5 documentation. (As @r-richards notes - change the version of the docs to v5 in the dropdown at the bottom of the left nav.)

2)将项目升级到Angular 6,以解决该问题

2) Upgrade your project to Angular 6 to fix the issue

遵循官方的Angular升级指南.

您将填写一份简短的表格,选择您所使用的Angular版本以及要升级到的版本.然后,它向您显示执行升级所需采取的步骤的列表.您应按照本指南进行所有升级. (请先导航然后离开):)

You'll fill out a short form selecting which version of Angular you are on and which version you want to upgrade to. It then shows you the list of necessary steps to take to perform the upgrade. You should follow this guide for all upgrades. (Please upvote before navigating away) :)

https://update.angular.io/

以下是我从Angular 5升级到Angular 6时要注意的一些事项.

您的里程可能会因您的配置而异.我链接到下面对我有帮助的几篇文章.

Your mileage may vary depending on your configuration. I linked to a couple articles below that helped me.

更新npm(节点程序包管理器)

Update npm (Node Package Manager)

npm install npm@latest -g 

Upgarde节点

对于Windows和Mac用户,最简单的方法是使用官方的Node安装程序.或参见下面有关linux/unix的文章.

For Windows and mac users, the easiest method is with the official Node installer. Or see the article below for linux/unix.

https://nodejs.org/en/download/

在全球和本地进行Upgarde Angular CLI

Upgarde Angular CLI globally and locally

npm install -g @angular/cli
npm install @angular/cli

更新package.json

Update package.json

ng update @angular/cli

更新@ Angular/Core

Update @Angular/Core

ng update @angular/core

使用时更新Angular材质

Update Angular Material if you're using it

ng update @angular/material

检查其他可能需要升级的软件包

Check for other packages that may need to be upgraded

npm outdated

更新所有过时的软件包

npm update

或升级特定软件包

npm update <package name>

您可能需要将angular-cli.json文件转换为angular.json

You may need to convert the angular-cli.json file to angular.json

ng update @angular/cli --migrate-only --from=1.7.4

参考:

升级节点

http://www.hostingadvice.com/how -to/update-node-js-latest-version/

升级Angular

https://vitalflux.com/upgrade-angular-5-app -angular-6/

其他故障排除步骤

https://stackoverflow.com/a/49811824/151325

这篇关于Angular Injectable装饰器-预期有0个参数,但有1个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆