Angular 2 bootstrap函数给出错误“参数类型AppComponent不能分配给参数类型Type”。 [英] Angular 2 bootstrap function gives error "Argument type AppComponent is not assignable to parameter type Type"

查看:272
本文介绍了Angular 2 bootstrap函数给出错误“参数类型AppComponent不能分配给参数类型Type”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个简单的 Hello World 来自



查看 bootstrap 函数声明, AppComponent 需要扩展类型

  export declare function bootstrap(appComponentType:Type,customProviders?:Array< any>):Promise< ComponentRef> ;; 

我的问题是:



Angular组件是否应该延长类型

解决方案

实际上, AppComponent应该扩展Type 。因此,请使用以下内容:

  // app.component.ts 
从'@ angular2导入{Component,Type} /核心';

@Component({
...
...
})
导出类AppComponent扩展类型{}

这也将遵循针对AppComponent(要进行自举)的ng2的预期约定。



我不确定为什么他们没有在ng2教程中介绍它,可能他们的目标是简化初始学习,因为即使没有 extends 部分,它也会运行。



这在WebStorm / IntelliJ中完全正常。



编辑:替代解决方案是更新到Webstorm v2016.2(在评论时这是稳定的)。


Here is my first simple Hello World angular 2 app from Angular 2 quick start guide.

import {Component} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
@Component({
    selector: 'ng2-app',
    template: '<h1>My first Angular 2 App</h1>'
})
export class AppComponent { }


bootstrap(AppComponent);

The application runs fine when I run with npm start but my IntelliJ IDE is showing error in the line with bootstrap(AppComponent)

Argument type AppComponent is not assignable to parameter type Type

Looking at the bootstrap function declaration, AppComponent needs to extends Type.

export declare function bootstrap(appComponentType: Type, customProviders?: Array<any>): Promise<ComponentRef>;

My question is:

Is it expected for Angular components to extend Type?

解决方案

Actually, AppComponent is expected to extend Type. So use the following:

// app.component.ts
import { Component, Type } from '@angular2/core';

@Component({
...
...
})
export class AppComponent extends Type {}

This will also follow the expected convention by ng2 for AppComponent (to be bootstrapped).

I am not sure why they didn't cover it in the ng2 tutorials, probably they might be targeting to simplify the initial learning, as it runs even without the extends part.

This works perfectly fine in WebStorm/IntelliJ.

Edit: Alternate solution is to update to Webstorm v2016.2 (which is stable at the time of this comment).

这篇关于Angular 2 bootstrap函数给出错误“参数类型AppComponent不能分配给参数类型Type”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆