在Angular 2中更改网址而无需重定向 [英] Change url without redirect in Angular 2

查看:66
本文介绍了在Angular 2中更改网址而无需重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Angular 2 Web应用程序中,我想在浏览器中更改url页面.例如,我想将其更改为:

In my Angular 2 web application I would like to change the url page in the browser. For example I'd like to change it from:

http://localhost:63000/my-page

http://localhost:63000/您的页面

它不能重定向,没有页面加载,并且浏览器历史记录必须反映页面更改.

It must not redirect, no page load, and browser history must reflect the page change.

可以做到吗?

推荐答案

这可以通过以下角度4中可用的功能来实现

This can be achieved by below function available in angular 4

location.replaceState(path: string, query?: string): void;

在执行此操作之前,您需要通过注入组件或指令构造函数来获取位置对象.

Before doing this you need to get location object by injecting in your component or directive constructor.

  constructor(private location: Location)

并像这样导入Location类-

And importing Location class like this -

import { Location } from '@angular/common';

这篇关于在Angular 2中更改网址而无需重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆