如何在angular 2中设置模板url

如何在angular 2中设置模板url,angular,components,angular-components,Angular,Components,Angular Components,我使用@angular/cli创建了一个新的angular应用程序。在该应用程序模板中已经有应用程序组件。在该应用程序组件中,我想使用另一个名为user的组件。因此在@组件中有一个属性,名为templateUrl,也有人说只使用模板。那么我应该使用哪一种,哪一种是最好的。目前,我正在尝试使用templateUrl 认为这是我的app.component.ts @Component({ selector: 'app-root', templateUrl: './app.component.

我使用
@angular/cli
创建了一个新的angular应用程序。在该应用程序模板中已经有
应用程序组件
。在该应用程序组件中,我想使用另一个名为
user
组件。因此在
@组件
中有一个
属性
,名为
templateUrl
,也有人说只使用
模板
。那么我应该使用哪一种,哪一种是最好的。目前,我正在尝试使用
templateUrl

认为这是我的
app.component.ts

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.css']
})
这是我的
user.component.ts

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.css']
})
那么,如何将我的
用户
组件与
应用程序组件
一起使用呢。我试图给出
相对路径
,但它不起作用,而给出
绝对路径
也不起作用。我很困惑。希望你能帮上忙

将我的用户组件与应用程序组件一起使用。 如果要将用户组件与应用程序组件一起使用,则

  • 在app的模板中添加用户组件 所以你的app.template。html将包含

    将我的用户组件与应用程序组件一起使用。 如果要将用户组件与应用程序组件一起使用,则

    • 在app的模板中添加用户组件 所以你的app.template。html将包含

      template url
      ,只需使用my
      app组件中的选择器,如下所示

      <div class="row">
          <app-user></app-user>
      </div>
      
      在我的
      app.component.html
      中,我们应该使用
      user
      component
      selector
      名称,如下所示

      <div class="row">
          <app-user></app-user>
      </div>
      
      
      

      这将显示
      应用程序组件内的
      用户组件
      内容

      模板url
      无关,只需使用我的
      应用程序组件内的选择器
      ,如下所示

      <div class="row">
          <app-user></app-user>
      </div>
      
      在我的
      app.component.html
      中,我们应该使用
      user
      component
      selector
      名称,如下所示

      <div class="row">
          <app-user></app-user>
      </div>
      
      
      

      这将在您的
      user.component.ts
      文件中显示
      用户组件
      app.component.ts
      应用组件

      名称
      选择器
      中的内容,并在
      app.component.ts
      文件中的
      模板
      中使用它。然后您可以使用这两个组件

      user.component.ts

      @Component({
        selector: 'app-user',
        templateUrl: './user.component.html', 
      })
      
      @Component({
        selector: 'app-root',
        template:`<app-user></app-user>`,
      })
      
      app.component.ts

      @Component({
        selector: 'app-user',
        templateUrl: './user.component.html', 
      })
      
      @Component({
        selector: 'app-root',
        template:`<app-user></app-user>`,
      })
      

      user.component.ts
      文件中命名
      selector
      ,并在
      app.component.ts
      文件中的
      模板中使用它。然后您可以使用这两个组件

      user.component.ts

      @Component({
        selector: 'app-user',
        templateUrl: './user.component.html', 
      })
      
      @Component({
        selector: 'app-root',
        template:`<app-user></app-user>`,
      })
      
      app.component.ts

      @Component({
        selector: 'app-user',
        templateUrl: './user.component.html', 
      })
      
      @Component({
        selector: 'app-root',
        template:`<app-user></app-user>`,
      })
      

      如果您想在应用程序组件中使用用户组件,那么在应用程序的模板中使用
      。所以
      templateUrl
      很多时候,最好为模板设置一个单独的文件,以便更容易编辑和维护。。。U使用templateUrl提供文件的链接。。。如果要添加内联html,请使用template如果要在应用程序组件中使用用户组件,请在应用程序的模板中使用
      。因此
      模板URL
      在许多情况下,最好为模板使用单独的文件,以便更容易编辑和维护。。。U使用templateUrl提供文件的链接。。。如果要添加内联html,请使用Template我想将其用作
      templateUrl
      我不想
      ->您不想使用templateUrl吗?或者你想使用模板url?对不起我的不好。输入错误我想将其用作
      templateUrl
      我不想
      ->你不想使用templateUrl吗?或者你想使用模板url?对不起我的不好。键入mistakewith template属性需要在.ts文件中添加html组件,但在templateUrl中可以提供html文件的链接,在该链接中可以为html组件保留单独的文件。使用template属性需要在.ts文件中添加html组件,但是在templateUrl中,您可以提供html文件的链接,在这里您可以为html组件保留一个单独的文件。