如何在angular2中使用子域进行路由? [英] how to route using subdomain in angular2?

查看:31
本文介绍了如何在angular2中使用子域进行路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望创建一个具有不同用户登录页面的 Angular 2 应用,例如user1.example-domain.com"和user2.example-domain.com"将具有不同且不相关的主页".

I'm looking to create an Angular 2 app with different user landing pages, for example 'user1.example-domain.com' and 'user2.example-domain.com' will have different and unrelated 'home page'.

我的问题:

  1. 我如何使用 Angular 2 做到这一点?
  2. 如何在我的本地机器上测试它?user1.localhost:port"够了吗?

我试过像这样使用静态路由:

I've tried using static routing like so:

 {path: 'test.localhost:4200/generic-link1', component: GenericLink1Component}

我已经看到它在不倒翁中完成,我知道它可以完成.

I've seen it done in tumbler I know that it could be done.

推荐答案

这很容易做到,但不能使用 Angular(或任何其他 JavaScript 路由器).路由到子域需要服务器帮忙.您需要将 user1user2 设置为子域,然后在您的默认文档中,根据您的规则路由到任何一个.

This can easily be done, but not with Angular (or any other JavaScript router for that matter). Routing to a sub-domain requires the server to help out. You need to set up user1 and user2 as sub-domains and then in your default document, route to either based on whatever you rules are.

您的 Angular 应用程序将驻留在任一地址,因此 http://user1 将有一个.example.com/index.html 和另一个位于 http://user2.example.com.它也可以是同一个应用.

You Angular app will then reside at either address, so there will be one at http://user1.example.com/index.html and another at http://user2.example.com. It could be the same app as well.

对于全 Angular 解决方案,您只需将方案更改为使用 http://example.com/user1.

For an all-Angular solution, you should simply change your scheme to use http://example.com/user1.

后一种 URL 方案可能更适合您的另一个原因.如果您是自托管,您可以根据自己的喜好创建子域.但是,如果您使用的是共享主机,则您可以使用的子域数量很可能会受到限制 - 可能只有两个.

There is one more reason why the latter URL scheme may be better for you. If you self-host, you can create sub-domains to your heart's content. If, however, you're using shared hosting, there is a pretty high likelihood that you'll be limited to the number of sub-domains you can use - it may be as few as two.

这篇关于如何在angular2中使用子域进行路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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