如何在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
,只需使用myapp组件中的选择器,如下所示
<div class="row">
<app-user></app-user>
</div>
在我的app.component.html
中,我们应该使用user
componentselector
名称,如下所示
<div class="row">
<app-user></app-user>
</div>
这将显示应用程序组件内的用户组件
内容
与模板url
无关,只需使用我的应用程序组件内的选择器
,如下所示
<div class="row">
<app-user></app-user>
</div>
在我的app.component.html
中,我们应该使用user
componentselector
名称,如下所示
<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组件保留一个单独的文件。