1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

angular-splitを利用する最小サンプルコード

Posted at

angular-splitの日本語記事がない!

angular-splitとは、ビューを分割し、ドラッグで分割領域を調節することができるUIライブラリです。(ほぼ公式文直訳)

便利で欠かせないライブラリなんですけど、日本語で解説してる記事がどうやらない。
Qiitaも個人ブログの記事も見つかりません。
それなら集合知を高めるためにも、私が記事を書こうかな~と思った次第です。
しかし私も触ったばかりなので、他の人がangular-splitを使用するハードルを下げるべくangular-splitを用いた最もシンプルなコードを書こうと思います。

まずはインストール

ng newでAngularの雛形を作成したら、

npm install angular-split

を実行。すぐ終わります。

そしてモジュール追加

app.module.ts
import { AngularSplitModule } from 'angular-split';

app.module.tsの先頭に↑を追加。

app.module.ts
AngularSplitModule.forRoot()

@NgModuleのimportsに↑を追加。

それからテンプレートを書く

app.component.html
<div class="split-example">
  <as-split [direction]="direction">
    <as-split-area #area1="asSplitArea">
      <p id="left"></p>
    </as-split-area>
    <as-split-area #area2="asSplitArea">
      <p id="right"></p>
    </as-split-area>
  </as-split>
</div>

次はコンポーネントを書く

app.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  direction: string = 'horizontal';

  constructor() {}
}

最期にスタイルシートだ

app.component.scss
.split-example {
    height: 100px;
    width: 200px;
}

# left {
    background-color: #cbffff;
}

# right {
    background-color: #ffffc7;
}

よし表示してみよう!

1-split.png
こんな感じになりますね。
中央の仕切りをドラッグして左右領域のサイズを変えることができますよ~。
angular-split様の御業です。

ただこれでは如何せんシンプルすぎない?という方は、
htmlに


<br />
<button class="btn btn-warning" (click)="direction = (direction === 'horizontal') ? 'vertical' : 'horizontal'">{{ '現在の分割: ' + direction + '' }}</button>

を追加し、

スタイルシートの.split-exampleに


outline: 5px dashed red;

を追加してみましょう。

2-split.png
枠ができてわかりやすくなりました。
ボタンを押すと分割方法が変わります。
3-split.png
やったね!

この超ゆとり記事を足掛かりに公式のサンプルで勉強してくれたらうれしいです。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?