2017-07-22 4 views
1

角度のある素材からスナックバーを使用しようとしましたが、問題が発生しました。スナップバーは画面のボタンに表示されず、ボタンの下に表示されますそれ。もう一つは、それはまた、私のページ上のスナックバーからのテキストを置きます。角度のスナックバーに関する問題

Snackbar.ts

import { Component, OnInit } from '@angular/core'; 
import {MdSnackBar} from '@angular/material'; 

@Component({ 
    selector: 'app-snack-bar', 
    templateUrl: './snack-bar.component.html', 
    styleUrls: ['./snack-bar.component.css'] 
}) 
export class SnackBarComponent implements OnInit { 

    constructor(public snackBar: MdSnackBar) {} 

    ngOnInit() { 
    } 

    openSnackBar() { 
    this.snackBar.open("Hello World","action",{duration: 500}); 
    } 
} 

Snackbar.html

<button md-button (click)="openSnackBar()" aria-label="Show an example snack-bar"> 
    Click me 
</button> 

app.component.ts

import { Component } from '@angular/core'; 
import { SnackBarComponent } from './components/snack-bar/snack-bar.component'; 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 
export class AppComponent { 

} 

ありがとう:enter image description here

ここで私が使用したコードです!

+0

はあなたを持っているあなたのindex.htmlで次の追加してテストを実行します。SnackBarModuleをインポート?マテリアルテーマを構成しましたか? Snackbar.htmlではなくsnack-bard.component.htmlを意味しましたか?他のcssライブラリ/ファイルを使用していますか? –

+0

はい、app.module.tsにMdSnackBarModuleをインポートしました。ドキュメンテーションはそれについて何も言わなかったので、私は公式資料ページにあったものを正確にたどったので、私は重要なテーマを構成しませんでした。 – Keselme

答えて

3

プロジェクトで構築済みテーマの一つをインポートする必要があります。私はあらかじめ作成されたテーマなしで試してみましたが、スナックバーはあなたが言及した問題を引き起こしていました。

Plunker without prebuilt theme

私はこれを追加したら、plunkerでのスナックバーがうまく働きました。

head

<link href="https://unpkg.com/@angular/material/prebuilt-themes/indigo-pink.css" rel="stylesheet"> 
+0

ありがとうございます!それが問題を解決しました! – Keselme

0

は、あなたのCSSに追加してみてください。

body { 
    height:100%; 
} 
+0

これは必要ではありません... –

関連する問題