Angular 2.0深层链接不起作用。与地址栏

时间:2017-02-21 07:12:18

标签: node.js angular routing deep-linking

当我在应用程序中构建和导航链接但在地址栏中键入时,一切正常! 最终我希望能够通过电子邮件发送指向特定应用页面/路径的链接,我不知道如何实现这一目标。

我已经关注了angular router guide documentation。我认为正确......

我正在使用NodeJS(带快递),而在服务器中,我已将所有流量重定向到应用程序根目录。

app.get("*", function(req, res) {
    console.error("in get *")
    res.redirect("/");
});

在我的index.html中,我设置了基础

<base href="/">

我的客户端路径/路径设置如下

const appRoutes: Routes = [
  { path: 'vendor/registration', component: VendorRegistrationComponent },
  { path: 'vendors', component: VendorListComponent },
  { path: '',  redirectTo: 'vendor/registration', pathMatch: 'full' },
  { path: '**', component: PageNotFoundComponent }    
];

如果我在浏览器地址栏中输入http://localhost:8080/vendors,我会看到http://localhost:8080/vendor/registration哪种有意义,因为这是服务器告诉浏览器重定向到的地方。

我应该如何深入链接到我的应用程序?

编辑。 Angular教程 - “英雄之旅” - 也表现出相同的行为。即直接在浏览器的地址栏中输入网址不起作用。该应用程序显示“正在加载...”文本,但不会路由到控制器。

2 个答案:

答案 0 :(得分:2)

通常,您不需要在服务器上重定向,因为您的所有路由都由客户端上的角度路由器处理。

相反,让您的全部快速路线始终为所有请求的网址提供index.html。当客户端应用程序引导时,angular将根据请求的URL来处理到相应组件的路由。请务必保留<base href="/">标记,以便有角度可以知道网址的哪一部分被路由。

const path = require('path');

app.get("*", function(req, res) {
    res.sendFile(path.join(__dirname, '/path/to/your/index.html'));
});

答案 1 :(得分:0)

您的问题的回答会在angular2 documentation

中明确说明