fix(core): require 'static' flag on queries in typings (#30639)

This commit makes the static flag on @ViewChild and @ContentChild required.

BREAKING CHANGE:

In Angular version 8, it's required that all @ViewChild and @ContentChild
queries have a 'static' flag specifying whether the query is 'static' or
'dynamic'. The compiler previously sorted queries automatically, but in
8.0 developers are required to explicitly specify which behavior is wanted.
This is a temporary requirement as part of a migration; see
https://angular.io/guide/static-query-migration for more details.

@ViewChildren and @ContentChildren queries are always dynamic, and so are
unaffected.

PR Close #30639
This commit is contained in:
Alex Rickabaugh
2019-05-23 11:31:10 -07:00
committed by Matias Niemelä
parent dc6406e5e8
commit 84dd2679a9
21 changed files with 46 additions and 45 deletions

View File

@ -15,7 +15,7 @@ export class CompForChildQuery {
@Component(
{selector: 'comp-with-child-query', template: '<comp-for-child-query></comp-for-child-query>'})
export class CompWithChildQuery {
@ViewChild(CompForChildQuery) child: CompForChildQuery;
@ViewChild(CompForChildQuery, {static: true}) child: CompForChildQuery;
@ViewChildren(CompForChildQuery) children: QueryList<CompForChildQuery>;
}

View File

@ -29,7 +29,7 @@ export interface Query {
read: any;
isViewQuery: boolean;
selector: any;
static?: boolean;
static: boolean;
}
export const createContentChildren = makeMetadataFactory<Query>(

View File

@ -102,7 +102,7 @@ export interface Query {
read: any;
isViewQuery: boolean;
selector: any;
static?: boolean;
static: boolean;
}
/**
@ -218,8 +218,8 @@ export interface ContentChildDecorator {
*
* @Annotation
*/
(selector: Type<any>|Function|string, opts?: {read?: any, static?: boolean}): any;
new (selector: Type<any>|Function|string, opts?: {read?: any, static?: boolean}): ContentChild;
(selector: Type<any>|Function|string, opts: {read?: any, static: boolean}): any;
new (selector: Type<any>|Function|string, opts: {read?: any, static: boolean}): ContentChild;
}
/**
@ -350,8 +350,8 @@ export interface ViewChildDecorator {
*
* @Annotation
*/
(selector: Type<any>|Function|string, opts?: {read?: any, static?: boolean}): any;
new (selector: Type<any>|Function|string, opts?: {read?: any, static?: boolean}): ViewChild;
(selector: Type<any>|Function|string, opts: {read?: any, static: boolean}): any;
new (selector: Type<any>|Function|string, opts: {read?: any, static: boolean}): ViewChild;
}
/**

View File

@ -52,7 +52,8 @@ describe('jit directive helper functions', () => {
descendants: false,
first: false,
isViewQuery: false,
read: undefined
read: undefined,
static: false,
})).toEqual({
propertyName: 'propName',
predicate: ['localRef'],
@ -69,7 +70,8 @@ describe('jit directive helper functions', () => {
descendants: true,
first: true,
isViewQuery: true,
read: undefined
read: undefined,
static: false,
})).toEqual({
propertyName: 'propName',
predicate: ['foo', 'bar', 'baz'],

View File

@ -168,7 +168,7 @@ import {el} from '../../testing/src/browser_util';
})
class Cmp {
exp: any;
@ViewChild('elm') public element: any;
@ViewChild('elm', {static: false}) public element: any;
}
TestBed.configureTestingModule({
@ -213,11 +213,11 @@ import {el} from '../../testing/src/browser_util';
exp2: any = true;
exp3: any = true;
@ViewChild('elm1') public elm1: any;
@ViewChild('elm1', {static: false}) public elm1: any;
@ViewChild('elm2') public elm2: any;
@ViewChild('elm2', {static: false}) public elm2: any;
@ViewChild('elm3') public elm3: any;
@ViewChild('elm3', {static: false}) public elm3: any;
}
TestBed.configureTestingModule({

View File

@ -51,7 +51,7 @@ import {el} from '../../testing/src/browser_util';
template: '...',
})
class Cmp {
@ViewChild('target') public target: any;
@ViewChild('target', {static: false}) public target: any;
constructor(public builder: AnimationBuilder) {}

View File

@ -73,9 +73,10 @@ describe('Integration', () => {
})
class ComponentWithRouterLink {
// TODO(issue/24571): remove '!'.
@ViewChild(TemplateRef) templateRef !: TemplateRef<any>;
@ViewChild(TemplateRef, {static: true}) templateRef !: TemplateRef<any>;
// TODO(issue/24571): remove '!'.
@ViewChild('container', {read: ViewContainerRef}) container !: ViewContainerRef;
@ViewChild('container', {read: ViewContainerRef, static: true})
container !: ViewContainerRef;
addLink() {
this.container.createEmbeddedView(this.templateRef, {$implicit: '/simple'});