私はCSSクラスをクリックして動的に追加したい 体 タグ。だから私は DOCUMENT
角のオブジェクト。ここにコードがあります
import { DOCUMENT } from "@angular/platform-browser";
... component code
constructor( @Inject(DOCUMENT) private document: Document) { }
addClass() {
this.document.body.classList.add("any_class");
}
これはエラーを示しています
Metadata collected contains an error that will be reported at runtime: Could not resolve type Document.
[0] {"__symbolic":"error","message":"Could not resolve type","line":50,"character":53,"context":{"typeName":"Document"}}
[0] at node_modules@angularcompiler-clisrcmetadatacollector.js:664:27
[0] at Array.forEach (<anonymous>)
[0] at validateMetadata (D:QPP WorkspaceQWCQWCnode_modules@angularcompiler-clisrcmetadatacollector.js:652:42)
[0] at MetadataCollector.getMetadata (D:QPP WorkspaceQWCQWCnode_modules@angularcompiler-clisrcmetadatacollector.js:507:17)
[0] at LowerMetadataCache.getMetadataAndRequests (ProjectPathnode_modules@angularcompiler-clisrctransformerslower_expressions.js:264:39)
[0] at LowerMetadataCache.ensureMetadataAndRequests (ProjectPathnode_modules@angularcompiler-clisrctransformerslower_expressions.js:209:27)
[0] at LowerMetadataCache.getRequests (ProjectPathnode_modules@angularcompiler-clisrctransformerslower_expressions.js:204:21)
[0] at ProjectPathnode_modules@angularcompiler-clisrctransformerslower_expressions.js:146:36
[0] at ProjectPathnode_modulestypescriptlibtypescript.js:2601:86
[0] at reduceLeft (ProjectPathnode_modulestypescriptlibtypescript.js:2274:30)
error Command failed with exit code 2.
[0] at ChildProcess.exithandler (child_process.js:275:12)
[0] at emitTwo (events.js:126:13)
[0] at ChildProcess.emit (events.js:214:7)
[0] at maybeClose (internal/child_process.js:925:16)
[0] at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
問題は何ですか?使ってます 角度5。クラスを動的に追加する他の方法はありますか?
回答:
回答№1は2あなたは直接使用することができます document
どんなインポートもしないで、タイプスクリプトに入れます。
ちょうど: document.body.classList.add("any_class");
.