1
0

More than 3 years have passed since last update.

AngularでブラウザURLを扱う

Posted at

Location

Angularにはアプリ側からブラウザのURL情報をやりとりできるLocationというサービスが予め用意されています。

使用例:前ページに戻る

ボタンをクリックするとブラウザ履歴の前ページに戻る実装をします。

some.component.ts
import { Location } from '@angular/common';

export class SomeComponent {
  constructor(
    private location: Location,
  ) { }

  cancel() {
    this.location.back();
  }
}
some.component.html
<button (click)="cancel()">
  キャンセル
</button>

使用例:現在のルートパスを取得する

some.component.ts
path: string;

this.path = this.location.path();

使用例:履歴を変えずにブラウザのURLを置き換え

こちらがわかりやすいです。
Angularで履歴を変えずにブラウザのURLを置き換える

参考

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