Angular2とtypescriptのどちらも比較的新しいです。 typescriptはjavascriptのスーパーセットなので、私はconsole.log
のような関数が動作すると期待しています。 console.log
は、コンポーネントクラスの外では.ts
のファイルでは正常に動作しますが、コンポーネントクラスの内側から期待されるように動作しません。console.logがAngular2で機能しないComponent(Typescript)
// main.ts
import { Component } from '@angular/core';
console.log("Hello1"); //1. This works perfectly
@Component({..)
export class App {
s: string = "Hello2";
// console.log(s); //2. This gives compilation error (when uncommented)
// Error: Function implementation is missing or not immediately following the declaration.
}
紛失しているものはありますか?
あなたは@Componentに何を入れましたか? 次のようなクラスにコンストラクタを入れてみることができますか? コンストラクタ(){console.log( 'test')} –
console.logが関数にラップされていないので、動作しないと思います。あなたはJSコンパイルされたフォームを表示できますか? –
@ L.querter:console.logはコンストラクタ内で使用されているときには動作しますが、コンストラクタが存在する場合でもコンストラクタの外側で使用された場合は機能しません。たぶんそれを関数にラップする必要があります。私はこれを知らなかった。 –