【问题标题】:Angular 2+ path to static html静态html的Angular 2+路径
【发布时间】:2018-10-24 10:29:23
【问题描述】:

在我的 Angular 2+ 应用程序之一中,我需要显示静态 html 文件内容。所以我把那个静态 html (helper.html) 放在 assets 文件夹中,我可以通过以下方式访问它:

http://localhost:4200/assets/helper.html

但是一旦我引入路由模块,我就会收到以下错误:

Error: Cannot match any routes. URL Segment: 'assets/static/helper.html' noMatchError

我需要在路由模块中做什么才能让这条路径正常工作?

PS:下面是我的路由模块的sn-p:

const routes: Routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full'},
  { path: 'home', component: HomeComponent }
]

@NgModule({
  imports: [ RouterModule.forRoot(routes) ],
  exports: [ RouterModule ]
})
export class AppRoutingModule { }

【问题讨论】:

  • 你必须像动态页面一样将它添加到 routing.module
  • 如果您也可以发布您的 routing.module,这将有助于回答
  • @hana_wujira 你能告诉我如何将它添加到路由模块吗?

标签: angular angular-routing angular-router


【解决方案1】:

此 html 必须带有组件。没有它就没有办法。

【讨论】:

    【解决方案2】:

    为其添加一个组件文件并为其设置一个url

    静态组件

    import {Component} from '@angular/core'
    @Component({
        templateUrl:'helper.html'
    })
    export class StaticComponent{
    }
    

    app-routing.module.ts

    import {StaticComponet} from '../assets/static.component.ts';
    const routes: Routes = [
      { path: '', redirectTo: 'home', pathMatch: 'full'},
      { path: 'home', component: HomeComponent },
    {path:'helper',component:StaticComponent}
    ]
    
    @NgModule({
      imports: [ RouterModule.forRoot(routes) ],
      exports: [ RouterModule ]
    })
    export class AppRoutingModule { }
    

    【讨论】:

    • 抱歉,这个方法不能用,因为helper.html有自己的<html><body>标签
    猜你喜欢
    • 2017-01-12
    • 1970-01-01
    • 2018-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    相关资源
    最近更新 更多