Angular 2.0。区别 @View @Component

Angular 2.0. Difference @View @Component

你好。我是 Angular 的新手。我正在测试 Angular 2.0.

我读了教程 here and the guide here。在 tuto 中,模板在 @Component 注释中指定,而在指南中它在 @View 注释中。所以我想知道这两种方法有什么区别?我在 api 预览中查找,但解释不清楚。

更新

@View() 被删除了(我认为在 beta.13 中,CHANGELOG.md 没有提到它)。

原创

它们之间没有区别。您可以将所有视图配置指定到 Component 中,这样就不需要导入 View 装饰器了。

但同时需要保留 View 装饰器的存在,因为它允许我们根据语言或媒体类型对同一组件使用不同的视图。例如:

@Component(/* ... */)
@View({
  media: 'desktop',
  template: 'Template for desktop'
})
@View({
  media: 'mobile',
  template: 'Template for mobile'
})
extends class Component() {}

此功能尚未实现。

正如@Alexpods 在回答和@Eric 之前的评论中所说,当 angular2 处于 alpha 状态时,@view 是可选的,因为 @view 注释中的所有属性也包含在 @component 注释中,所以 @view 只是sugar,你可以将所有视图配置指定到组件中,这样就不需要导入视图装饰器了。

已更新为测试版

@View 已在最新版本中弃用,因此您不能使用它。

如果您仍在使用@view 注释,则可能会导致产生某种错误。所以只有组件是保存所有选项的地方。

根据官方消息,@View 元数据装饰器已在 beta.10 版本中弃用。

根据 Angular v2.0.0-beta.11 的 ChangeLogs,在重大更改中提到 @View() 注释(之前已弃用)已被删除。应用程序应改用 @Component() 装饰器。

请查看 Angular2 here.

的更改日志

首先,这个 已被弃用 ,现在完全 消失了

2.0.0-beta.10 (2016-03-17):@View() 注释(之前已弃用)已被删除。应用应改用 @Component() 装饰器。

所以你不用再担心了,之前引入@View是因为早期的想法是一个组件中可以有多个视图(例如移动视图)并且用法是以下:

    import { Component } from '@angular/core';

    @Component({
      selector: 'app-root',
      styleUrls: ['./app.component.scss']})
    @View({
      media: 'desktop',
      template: '<h1>tablet<h1>'
    })
    @View({
      media: 'mobile',
      template: '<h1>mobile<h1>'
    })

    extends class Component() {}