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

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

问题描述

我正在使用Angular9.我不想在应用程序中包含根目录中的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天全站免登陆