Angular 2 @View with template vs @Component with template

时间:2016-01-29 19:48:47

标签: javascript angular

我希望有人能为我解决这个问题。我假设有一个简单的答案。但如果它出现在互联网上,我似乎无法找到它。请记住,我的角色很少,所以如果我遗漏了一些明显的东西,我会道歉。

我已经看到以下用于在Angular 2中创建模板的示例。

//Example 1

@Component({
   selector: 'my-app'
})

@View({
  template : '<p></p>'
})

据我了解,上面的例子与此相同。

// Example 2

@Component({
   selector: 'my-app',
   template: '<p></p>'
})

根据此https://angular.io/docs/ts/latest/api/core/View-decorator.html和此@Directive v/s @Component in angular2

那么有没有什么好的理由来创建第一个示例的模板,而第二个@View似乎是通过@Component使用“template:”隐式设置?

1 个答案:

答案 0 :(得分:1)

我认为你应该看一下这个问题:Angular 2.0. Difference @View @Component

您的example1和example2实际上做同样的事情......

事实上,它是相同的,因为View是可选的,但将来,您将能够为同一个组件定义多个视图。

希望它可以帮到你, 亨利