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

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

问题描述

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

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

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

ng 生成服务英雄

在这个生成的服务中,你有一个像这样的装饰器:

@Injectable()

然后他们要求向该装饰器添加一个参数,如下所示:

@Injectable({提供在:'根'})

当我尝试这样做时,我有一个 TypeScript 错误告诉我:

<块引用>

错误 TS2554:需要 0 个参数,但得到 1 个.我使用的版本是那些:

Angular CLI:1.7.4节点:9.5.0操作系统:win32 x64角度:5.2.10... 动画、通用、编译器、编译器-cli、核心、表单... http、语言服务、平台浏览器... 平台浏览器动态,路由器@角度/cli:1.7.4@angular-devkit/build-optimizer: 0.3.2@angular-devkit/核心:0.3.2@angular-devkit/schematics: 0.3.2@ngtools/json-schema: 1.2.0@ngtools/webpack: 1.10.2@原理图/角度:0.3.2@schematics/package-update: 0.3.2打字稿:2.5.3网络包:3.11.0

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

提前致谢!

解决方案

这是 Angular 6 的新特性.

@Injectable({提供在:'根'})

您有两个选择:

  1. 请参阅 Angular 5 文档.(正如@r-richards 指出的那样 - 在左侧导航底部的下拉列表中将文档版本更改为 v5.)

  2. 将您的项目升级到 Angular 6 以解决问题

遵循官方的 Angular 升级指南.

您将填写一个简短的表格,选择您使用的 Angular 版本以及您想要升级到的版本.然后,它会向您显示执行升级所需的必要步骤列表.您应该按照本指南进行所有升级.

以下是我从 Angular 5 升级到 Angular 6 时的一些笔记.

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

更新 npm(节点包管理器)

npm install npm@latest -g

升级节点

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

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

在全球和本地升级 Angular CLI

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

更新 package.json

ng 更新@angular/cli

更新@Angular/Core

ng 更新 @angular/core

如果您正在使用它,请更新 Angular Material

ng 更新@angular/material

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

npm 已过时

更新所有过时的软件包

npm 更新

或者升级一个特定的包

npm update <包名>

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

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

正如@Sharondio 指出的那样,如果您看到奇怪的现象,此时重新启动您的 IDE,甚至可能是您的整个计算机都没有什么坏处.

参考文献:

升级节点

http://www.hostingadvice.com/操作方法/更新节点-js-最新版本/

升级 Angular

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

其他故障排除步骤

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

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

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'
})

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

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 ?

Thanks in advance !

解决方案

This is a new feature in Angular 6.

@Injectable({
  providedIn: 'root'
})

You have two options:

  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. Upgrade your project to Angular 6 to fix the issue

Follow the official Angular upgrade guide.

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.

https://update.angular.io/

Here are some notes I took while upgrading from Angular 5 to Angular 6.

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

Update npm (Node Package Manager)

npm install npm@latest -g 

Upgarde node

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 globally and locally

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

Update package.json

ng update @angular/cli

Update @Angular/Core

ng update @angular/core

Update Angular Material if you're using it

ng update @angular/material

Check for other packages that may need to be upgraded

npm outdated

Update all outdated packages

npm update

Or to upgrade a specific package

npm update <package name>

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

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

And as @Sharondio notes, it never hurts to restart your IDE, and possibly your entire computer at this point if you're seeing weirdisms.

References:

Upgrading Node

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

Upgrading Angular

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

Other Troubleshooting Steps

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

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

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