[英]How can Angular 4 change URL when routing without “#” and no side effects?
In previous versions of angular, or other SPA frameworks, navigations used to use "#"
in the URL to move from a page to another without make another request to the server. 在angular或其他SPA框架的早期版本中,导航曾经使用URL中的
"#"
从一个页面移动到另一个页面,而无需再次向服务器发出请求。
Today, using Angular 4 routes, I notice that the URL was rewritten without any request to the server. 今天,使用Angular 4路由,我注意到URL被重写,而没有任何对服务器的请求。 for instance
domain.com/about
. 例如
domain.com/about
。 It seems very strange to me. 对我来说似乎很奇怪。
If my SPA is served from a domain.com/index.html
, a request to domain.com/about
should have a side effect bring a totally diferente page (for instance about/index.hmtl
), not a route inside the index.html
. 如果我的SPA是从
domain.com/index.html
,则对domain.com/about
的请求应具有副作用,这会导致页面完全不同(例如about/index.hmtl
),而不是index.html
内部的路由index.html
。
Change URL without redirect to the address in the URL seems to violate something.. dont? 更改URL而不重定向到URL中的地址似乎违反了某些规定。
How Is that possible in Angular 4 using only client side? 仅使用客户端,在Angular 4中怎么可能?
While you navigate on site, click links, tabs etc. - this all is just client side. 当您在网站上浏览时,单击链接,选项卡等-所有这些都只是客户端。 However, when you press F5 or navigate to link like /school/6/personList - server should be configured to return index.html.
但是,当您按F5键或导航至/ school / 6 / personList之类的链接时-服务器应配置为返回index.html。
Thats why quite often rest api is smth like /api/users, /api/emails, etc. so all start with '/api' - and you may easily redirect all other requests to index.html. 这就是为什么休息api经常像/ api / users,/ api / emails等一样,所以都以'/ api'开头的原因-您可以轻松地将所有其他请求重定向到index.html。
Change URL without redirect to the address in the URL seems to violate something.. dont?
更改URL而不重定向到URL中的地址似乎违反了某些规定。
No, that's the point of Single Page Applications (SPA) as opposed to the other way where a request is sent to the server for each URL change. 不,这是单页应用程序(SPA)的要点,与之相反,对于每个URL更改,将请求发送到服务器。
Angular router has two location strategies: 角路由器有两种定位策略:
navigations used to use "#" in the URL to move from a page to another without make another request to the server.
导航曾经使用URL中的“#”从一个页面移动到另一个页面,而不向服务器发出另一个请求。
It's still possible now with Angular by using HashLocationStrategy
, to do that just use {useHash: true}
do the following: Angular现在仍然可以通过使用
HashLocationStrategy
来做到这一点,只需使用{useHash: true}
执行以下操作:
@NgModule({
imports: [
RouterModule.forRoot(appRoutes, {useHash: true})
],
Today, using Angular 4 routes, I notice that the URL was rewritten without any request to the server
今天,使用Angular 4路由,我注意到URL被重写,而没有对服务器的任何请求
That's because the PathLocationStrategy
is used by default. 这是因为默认情况下使用
PathLocationStrategy
。
Regardless of the strategy, if you're using Angular routing, all URL changes from the application will be handled by the router and no requests will be sent to the server. 不管采用哪种策略,如果您使用的是Angular路由,则来自应用程序的所有URL更改都将由路由器处理,并且不会将任何请求发送到服务器。 For example, the following code:
例如,以下代码:
Router.navigateByUrl('/my')
in the case of PathLocationStrategy
will generate the following URL: 对于
PathLocationStrategy
将生成以下URL:
host/my
and in the case of HashLocationStrategy
it will generated ULR like this: 在
HashLocationStrategy
的情况下,它将生成如下所示的HashLocationStrategy
:
host#my
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.