Javascript 错误 TS2315:类型“ElementRef”不是通用材料角度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/50024565/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error TS2315: Type 'ElementRef' is not generic material angular
提问by Nilson Nieto
ERROR in node_modules/@angular/material/button-toggle/typings/button-toggle.d.ts(136,20):
error TS2315: Type 'ElementRef' is not generic.
node_modules/@angular/material/button-toggle/typings/button-toggle.d.ts(154,104):
error TS2315: Type 'ElementRef' is not generic
node_modules/@angular/material/button-toggle/typings/button-toggle.d.ts(136,20) 中的错误:
错误 TS2315:类型“ElementRef”不是通用的。
node_modules/@angular/material/button-toggle/typings/button-toggle.d.ts(154,104):
错误 TS2315:类型“ElementRef”不是通用的
I'm trying to install material but it looks like that's the mistake.
我正在尝试安装材料,但看起来这是错误的。
DATA:
数据:
Angular CLI: 1.7.4
Node: 9.11.1
回答by Newtorn Moses
The best way here is to change all your material dependencies to match that of angular for those of you who have angular 5.2.0 follow this
这里最好的方法是更改所有材料依赖项以匹配 angular 的那些拥有 angular 5.2.0 的人,请遵循此
INITIAL
最初的
"dependencies": {
  "@angular/core": "^5.2.0",
  "@angular/cdk": "^6.0.1",
  "@angular/material": "^6.0.1"    
}
just change these to whatever the current version of the other angular components are.
只需将这些更改为其他角度组件的当前版本即可。
FINAL
最终的
"dependencies": {
  "@angular/core": "^5.2.0",
  "@angular/cdk": "^5.2.0",
  "@angular/material": "^5.2.0"    
}
THEN do npm install
然后做 npm install
回答by John Willson
This occurs when all of your angular packagage in one version and material package in another version.
当您的所有 angular 包在一个版本中而材料包在另一个版本中时,就会发生这种情况。
I faced this when my angular package version was 5.2 and material version was 6.0 so i changed the material version back to 5.1 and the issue is fixed.
当我的 angular 包版本是 5.2 并且材料版本是 6.0 时,我遇到了这个问题,所以我将材料版本改回 5.1 并且问题得到了解决。
回答by yehuda mazal
OK, here is how I fixed it: Step one:
好的,这是我修复它的方法:第一步:
- run 
npm update -Dandnpm update -SBut I don't think this was necessary, because the problem was stil there. 
- 运行
npm update -D和npm update -S但我认为这没有必要,因为问题仍然存在。 
Step tow:
步拖:
npm install -g @angular/cli@latest
ng update
The Command complainded that I needed to run (so I did):
命令抱怨我需要运行(所以我做了):
ng update @angular/cli
After trying: ng serveI got an error since 'hammerjs' was not installed, so I run (but this might not be your case):
尝试后:ng serve由于未安装“hammerjs”,因此出现错误,因此我运行(但这可能不是您的情况):
npm install hammerjs --save
and 
npm install @types/hammerjs --save-dev
和 
npm install @types/hammerjs --save-dev
I don't understand the point of --save-dev, but I did it this way. Its compiling now.
我不明白 --save-dev 的意义,但我是这样做的。它现在编译。
This last hammerjs thing has been found here: Module not found: Error: Can't resolve 'hammerjs'
在这里找到了最后一个hammerjs: Module not found: Error: Can't resolve 'hammerjs'
Hope this help.
希望这有帮助。
回答by Hamdalah
I had the same issues but downgrading my @angular/material to version 5.0 worked. You can try that out.
我遇到了同样的问题,但将我的 @angular/material 降级到 5.0 版有效。你可以试试看。
回答by ajay verma
This error occurs when there is a mismatch in your angular version and material version. To see the error go to package.json file where you can find the angular version and the material version.
当您的角度版本和材料版本不匹配时,会发生此错误。要查看错误,请转到 package.json 文件,您可以在其中找到角度版本和材料版本。
  "dependencies": {
"@angular/animations": "^5.2.0",
"@angular/cdk": "^6.2.1",              //ERROR here the version is 6.x
"@angular/common": "^5.2.0",
"@angular/compiler": "^5.2.0",
"@angular/core": "^5.2.0",
"@angular/forms": "^5.2.0",
"@angular/http": "^5.2.0",
"@angular/material": "^6.2.1",         // ERROR here the material version is 6.x
"@angular/platform-browser": "^5.2.0",
"@angular/platform-browser-dynamic": "^5.2.0",
"@angular/router": "^5.2.0",
"core-js": "^2.4.1",
"hammerjs": "^2.0.8",
"rxjs": "^5.5.6",
"zone.js": "^0.8.19"}
},
},
As you can see in the above package.json file there is mismatch of the version. There are two ways to solve this error:
正如您在上面的 package.json 文件中看到的,版本不匹配。有两种方法可以解决此错误:
- Upgrade your angular by using command: ng update(this will change your angular versio to 6.x).
 - Change the version of cdk and material to 5.2.0 manuallythen enter the command: npm installthis will add the material of version 5.2.0
 
- 使用命令升级您的角度: ng update(这会将您的角度版本更改为 6.x)。
 - 手动将cdk和material的版本改为5.2.0然后输入命令:npm install这会添加5.2.0版本的material
 
回答by yehuda mazal
Maybe its related to angular 1.6 that just came out ?
也许它与刚刚发布的 angular 1.6 有关?
I have the same problem, but just can't figure out how to update the reste of it (instead of downgrade one thing, upgrade everything else thing), but I can't figure it out.
我有同样的问题,但就是不知道如何更新它的其余部分(而不是降级一件事,升级其他所有东西),但我无法弄清楚。
Tried npm update -Dand than npm update -SBut it did not help.
试图npm update -D比npm update -S,但它并没有帮助。
That idea came from: I am new to angular. I just installed angular material and angular animations in my small project and got some of the errors
这个想法来自:我是 angular 的新手。我刚刚在我的小项目中安装了 angular material 和 angular animations 并得到了一些错误

