zoukankan      html  css  js  c++  java
  • [Angular2 Router] Understand the Angular 2 Base href Requirement

    The <base href=”/”/> you define will determine how all other assets you plan on loading treat their relative paths. While you’ll most often use / as your base href, it’s important to understand what’s going on in case you need to change how you’re hosting your project.

    If the base href is not set, app will have error. The easiest way to do it set base href in html:

    <!doctype html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>WikiSearch</title>
      <base href="/">
    
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="icon" type="image/x-icon" href="favicon.ico">
    
    </head>
    <body>
      <app-root>Loading...</app-root>
    </body>
    </html>

    Sometimes though, coders of an Angular application don’t have access to the head section of the
    application HTML. This is true for instance, when reusing headers and footers of a larger, preexisting
    application.
    Fortunately there is a workaround for this cases. You can declare the application base path
    programmatically, when bootstrapping the Angular application:

    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        appRoutes,
        BrowserModule,
        FormsModule,
        HttpModule,
        JsonpModule,
        SharedServiceModule.forRoot()
      ],
      providers: [
        {provide: APP_BASE_HREF, useValue: '/'},
        {
          provide: API_URL,
          useValue: `https://en.wikipedia.org/w/api.php?callback=JSONP_CALLBACK`
        }
      ],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
  • 相关阅读:
    CSS命名规范
    mysql对GIS空间数据的支持,包括创建空间索引
    jQuery和DOM对象
    教你轻松快速学会用Calibre TXT转MOBI
    教你如何写出一手漂亮的英文 ( 附视频教程 )
    推荐几个知名顶级的文献网站
    最常犯的12个逻辑错误
    信息学竞赛书籍
    适合入门的英语原版书
    2017十大流行编程挑战网站
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5909208.html
Copyright © 2011-2022 走看看