忽略根 RouterModule 中的路径 [英] Ignore a path in the root RouterModule

查看:25
本文介绍了忽略根 RouterModule 中的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Angular 9.我在根目录中有一个 JSON 文件,我不想将它包含在应用程序中.换句话说:

I am using Angular 9. I have a JSON file in the root directory that I do not want included in the app. In other words:

/src
  |-- /app
  |-- /assets
  |-- index.html
  |-- do_not_include_this_file.json

这意味着 http://example.com/do_not_include_this_file.json 应该被 RouterModule 忽略,而是直接转到文件.目前该路径将重定向到 PageNotFoundComponent,例如

This means http://example.com/do_not_include_this_file.json should be ignored by the RouterModule and instead go straight to the file. Currently that path will redirect to a PageNotFoundComponent, e.g.

export const APP_ROUTER_PROVIDERS: Routes = [
    { path: 'login', component: LoginComponent },
    { path: '', redirectTo: '/login', pathMatch: 'full' },
    { path: '**', component: PageNotFoundComponent }
];

如何让 RouterModule 完全忽略路径?

How can I get the RouterModule to completely ignore a path?

推荐答案

您可以将do_not_include_this_file.json"作为根目录中的静态文件提供.

You can serve "do_not_include_this_file.json" as a static file from your root directory.

检查 Angular 应用程序的根目录中是否有名为angular.json"的文件.在angular.json"中,您可以使用资产实体.您包含在资产值数组中的任何文件或目录都将静态提供.在此处添加您的静态文件.启用后,所有这些文件也将在生产环境中提供/复制到 dist 目录.

Check the root directory of your Angular application for a file called "angular.json". In "angular.json", you can use the assets entities. Any files or directories you include in the asset value array will be served statically. Add your static file here. When enabled, all of these files will also be served in a production environment / copied to the dist directory.

以下代码假定您的 src 文件夹中有do_not_include_this_file.json",并将其提供给输出的根目录.

"projects": {
  "YourProject": {
      "root": "",
      ...
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            ...
            "assets": [
              ...
              {
                "glob": "do_not_include_this_file.json",
                "input": "src/",
                "output": "/"
              },
              ...
            ]

这是一个了解更多信息的页面;https://angular.io/guide/workspace-config#asset-config

Here is a page for more information; https://angular.io/guide/workspace-config#asset-config

这篇关于忽略根 RouterModule 中的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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