Angular 2.0. Difference @View @Component

后端 未结 4 1978
一生所求
一生所求 2020-11-29 11:22

Hi. I\'m new to Angular. I\'m testing Angular 2.0.

I read the tuto here and the guide here. In the tuto, the template is specified in the @Component ann

相关标签:
4条回答
  • 2020-11-29 11:38

    First of all, this was deprecated and now fully gone!

    2.0.0-beta.10 (2016-03-17): @View() annotation (previously deprecated) has been removed. Apps should use the @Component() decorator instead.

    So you don't need to worry about it anymore, The @View was previously introduced because the early thinking was that there could be multiple views in a component (like a mobile view for example) and the usage was as below:

        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() {}
    
    0 讨论(0)
  • 2020-11-29 11:48

    As said by @Alexpods in answer and @Eric in comment before when angular2 is in alpha @view is juts optional because all the properties in the @view annotation is also included in the @component annotation so @view is just sugar that you can specify all view configuration into Component so there's no need to import View decorator.

    Updated to beta

    @View has been deprecated in latest release, So you can't use it.

    if you are using still @view annotation it may cause producing some kind of error. so Only Component is the place that will hold the all options.

    As per officials , @View metadata decorator has been deprecated in beta.10 release.

    0 讨论(0)
  • 2020-11-29 11:51

    Update

    @View() was removed (I think in beta.13, the CHANGELOG.md doesn't mention it though).

    Original

    There are no differences between them. It's just sugar that you can specify all view configuration into Component so there's no need to import View decorator.

    But at the same time there's a need to remain View decorator exist, because it allows us to use different views for the same component depending on language or media type. For example:

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

    This feature is not implemented yet.

    0 讨论(0)
  • 2020-11-29 12:03

    As per the ChangeLogs of Angular v2.0.0-beta.11, it is mentioned under breaking changes that @View() annotation (previously deprecated) has been removed. Apps should use the @Component() decorator instead.

    Please have a look in Change logs of Angular2 here.

    0 讨论(0)
提交回复
热议问题