私のアプリケーションは、このように構築されていますangular2で共有コンポーネントを使用する方法は?
- いくつかの基本的なコンポーネント(家を、ログイン、について、...)私のメイン app.moduleで
- アプリケーションの各部分のためのいくつかの他のモジュール
私はすべての場所で使用する必要があるコンポーネントToastComponentを持っています。 私は基本的なコンポーネントでうまく動作しますが、モジュールに含まれているコンポーネントではうまく動作しません。
しかし私は、サービスを共有する方法について多くのページを読んで(私はangular2に認めるすべてを理解していない)、私はまだそれは私の場合で動作するようにする方法を見つけ出すことはできません。 (私も共有モジュールを試しましたが、悪化しました)
私が持っている問題は、setMessage(コードの終わりを参照)に与えられたメッセージはコンソールに表示されますが、画面には表示されないということです!
誰かがそれをコード化する適切な方法を教えてくれることを願っています。 TIA
JP Iは、(多くの試行の後に)知られている段階に戻す後ここ
がコードされ...
ToastComponent:
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-toast',
templateUrl: './toast.component.html',
styleUrls: ['./toast.component.css']
})
export class ToastComponent {
@Input() message = { body: '', type: '' };
setMessage(body, type, time = 5000) {
this.message.body = body;
this.message.type = type;
console.log('ToastComponent %o' , this.message);
setTimeout(() => { this.message.body = ''; }, time);
}
}
app.module
import { ToastComponent } from './_shared/toast.component';
...
import { LivresModule } from './livres/livres.module'; // where toast will be used
import { ExampleComponent } from './example.component'; // where toast is working fine
...
@NgModule({
imports: [ ... , LivresModule ],
providers: [ ToastComponent, ... ],
declarations: [ AppComponent, ToastComponent, ... ]
exports: [ ToastComponent ],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ],
bootstrap: [ AppComponent ],
})
...
livres.module.ts
...
import { ToastComponent } from '../_shared/toast.component';
import { AuteursComponent } from './auteurs.component'; // where to use toast effectively
...
@NgModule({
...
providers: [ ToastComponent, ... ]
declarations: [
// ToastComponent, // error as declared twice with app.module : OK so commented
...
auteurs.component
...
import { ToastComponent } from '../_shared/toast.component';
...
constructor(private http: Http, private authorService: AuthorService,
private toast: ToastComponent) { }
...
addAuthor() {
...
this.toast.setMessage('Auteur ajouté', 'success');
}
auteurs.component.html
...<app-toast [message]="toast.message"></app-toast>...
auteurs.component。*は正常に動作しますapp.moduleの構成部品のコピーです。
NEW INFO: 私は私が構築しています1を模倣光アプリを再作成しています、と私は、モジュール内のページのために、このエラーが発生しました:
Unhandled Promise rejection: Template parse errors:
Can't bind to 'message' since it isn't a known property of 'app-toast'.
1. If 'app-toast' is an Angular component and it has 'message' input, then verify that it is part of this module.
2. If 'app-toast' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message.
("<app-toast [ERROR ->][message]="toast.message"></app-toast>
<p> page2 works! </p>
<button (click)="displayMsg()"> Click !"): [email protected]:11
私は "追加した理由ですスキーマ:[CUSTOM_ELEMENTS_SCHEMA] "(私はこれを忘れてしまった)が、実際には実行時にコンパイル時にエラーを隠している!
サービスに変換してもhtmlコードとcssは表示されません。
あなたはエラーについて話しますが、あなたの質問には何も含まれていません。 Plesseは正確なエラーメッセージを追加します。 –
エラーはまったくありません。setMessageを使用すると、画面に表示されないメッセージのみです。 – JPMous
トーストコードをサービスとして設定する必要があります。より良い実装についてはこちらをご覧ください:https://github.com/johnpapa/event-view –