将 Angular 2 升级到 angular 4 失败

Upgrade Angular 2 to angular 4 failed

我从 angular 2 升级到 angular 4,现在出现错误。我从 this 复制了文本 即

npm install @angular/common@latest @angular/compiler@latest @angular/compiler-cli@latest @angular/core@latest @angular/forms@latest @angular/http@latest @angular/platform-browser@latest @angular/platform-browser-dynamic@latest @angular/platform-server@latest @angular/router@latest @angular/animations@latest typescript@latest --save

现在我在 运行 npm start 时遇到这个错误 请协助

`

product-management@1.0.0 start C:\Users\dp9\Documents\Visual Studio 2015\Projects\NG\NG UI

tsc && concurrently "tsc -w" "lite-server"

node_modules/@types/node/index.d.ts(70,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'main' must be of type 'any', but here has type 'NodeModule'.

node_modules/@types/node/index.d.ts(81,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'parent' must be of type 'any', but here has type 'NodeModule'.

node_modules/@types/node/index.d.ts(82,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'children' must be of type 'any[]', but here has type 'NodeModule[]'.

node_modules/@types/node/index.d.ts(102,6): error TS2300: Duplicate identifier 'BufferEncoding'.

node_modules/@types/node/index.d.ts(263,18): error TS2300: Duplicate identifier 'EventEmitter'.

node_modules/@types/node/index.d.ts(371,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'stdout' must be of type 'WritableStream', but here has type 'WriteStream'.

node_modules/@types/node/index.d.ts(372,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'stderr' must be of type 'WritableStream', but here has type 'WriteStream'.

node_modules/@types/node/index.d.ts(373,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'stdin' must be of type 'ReadableStream', but here has type 'ReadStream'.

node_modules/@types/node/index.d.ts(422,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'platform' must be of type 'string', but here has type 'Platform'.

node_modules/@types/node/index.d.ts(580,26): error TS2300: Duplicate identifier 'Buffer'.

node_modules/@types/node/index.d.ts(580,50): error TS2300: Duplicate identifier 'SlowBuffer'.

node_modules/@types/node/index.d.ts(604,22): error TS2415: Class 'EventEmitter' incorrectly extends base class 'internal'. Types of property 'eventNames' are incompatible. Type '() => (string | symbol)[]' is not assignable to type '() => string[]'. Type '(string | symbol)[]' is not assignable to type 'string[]'.

node_modules/@types/node/index.d.ts(624,5): error TS2309: An export assignment cannot be used in a module with other exported elements.

node_modules/@types/node/index.d.ts(769,18): error TS2300: Duplicate identifier 'Agent'.

node_modules/@types/node/index.d.ts(824,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'addressType' must be of type 'string', but here has type 'number | "udp4" | "udp6"'.

node_modules/@types/node/index.d.ts(827,18): error TS2300: Duplicate identifier 'Worker'.

node_modules/@types/node/index.d.ts(1510,17): error TS2300: Duplicate identifier 'CompleterResult'.

node_modules/@types/node/index.d.ts(1547,18): error TS2300: Duplicate identifier 'Script'.

node_modules/@types/node/index.d.ts(2911,18): error TS2300: Duplicate identifier 'TLSSocket'.

node_modules/@types/node/index.d.ts(3108,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'pfx' must be of type 'any', but here has type 'string | Buffer[]'.

node_modules/@types/node/index.d.ts(3109,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'key' must be of type 'any', but here has type 'string | any[] | string[] | Buffer'.

node_modules/@types/node/index.d.ts(3111,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'cert' must be of type 'any', but here has type 'string | string[] | Buffer | Buffer[]'.

node_modules/@types/node/index.d.ts(3112,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'ca' must be of type 'any', but here has type 'string | string[] | Buffer | Buffer[]'.

node_modules/@types/node/index.d.ts(3113,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'crl' must be of type 'any', but here has type 'string | string[]'.

node_modules/@types/node/index.d.ts(3115,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'honorCipherOrder' must be of type 'any', but here has type 'boolean'.

node_modules/@types/node/index.d.ts(3118,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'NPNProtocols' must be of type 'any', but here has type 'string[] | Buffer'.

node_modules/@types/node/index.d.ts(3135,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'key' must be of type 'string | Buffer', but here has type 'string | string[] | Buffer | Buffer[]'.

node_modules/@types/node/index.d.ts(3137,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'cert' must be of type 'string | Buffer', but here has type 'string | string[] | Buffer | Buffer[]'.

node_modules/@types/node/index.d.ts(3138,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'ca' must be of type '(string | Buffer)[]', but here has type 'string | Buffer | (string | Buffer)[]'.

node_modules/@types/node/index.d.ts(3377,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'padding' must be of type 'any', but here has type 'number'.

node_modules/@types/node/index.d.ts(3382,9): error TS2403: Subsequent variable declarations must have the same type. Variable 'padding' must be of type 'any', but here has type 'number'.

node_modules/@types/node/index.d.ts(3429,22): error TS2420: Class 'Readable' incorrectly implements interface 'ReadableStream'. Types of property 'pause' are incompatible. Type '() => Readable' is not assignable to type '{ (): ReadableStream; (): ReadableStream; }'. Type 'Readable' is not assignable to type 'ReadableStream'. Types of property 'eventNames' are incompatible. Type '() => (string | symbol)[]' is not assignable to type '() => string[]'. Type '(string | symbol)[]' is not assignable to type 'string[]'. node_modules/@types/node/index.d.ts(3512,22): error TS2420: Class 'Writable' incorrectly implements interface 'WritableStream'. Types of property 'eventNames' are incompatible. Type '() => (string | symbol)[]' is not assignable to type '() => string[]'. Type '(string | symbol)[]' is not assignable to type 'string[]'. node_modules/@types/node/index.d.ts(3622,5): error TS2309: An export assignment cannot be used in a module with other exported elements.

node_modules/@types/node/index.d.ts(3705,5): error TS2300: Duplicate identifier 'export='.

node_modules/@types/node/index.d.ts(3727,18): error TS2300: Duplicate identifier 'Domain'.

node_modules/@types/node/index.d.ts(3727,18): error TS2420: Class 'Domain' incorrectly implements interface 'NodeJS.Domain'.

Types of property 'eventNames' are incompatible. Type '() => (string | symbol)[]' is not assignable to type '() => string[]'. Type '(string | symbol)[]' is not assignable to type 'string[]'.

node_modules/@types/node/index.d.ts(4019,5): error TS2300: Duplicate identifier 'export='.

typings/globals/node/index.d.ts(78,6): error TS2300: Duplicate identifier 'BufferEncoding'.

typings/globals/node/index.d.ts(234,18): error TS2300: Duplicate identifier 'EventEmitter'.

typings/globals/node/index.d.ts(516,9): error TS2502: 'BuffType' is referenced directly or indirectly in its own type annotation.

typings/globals/node/index.d.ts(517,9): error TS2502: 'SlowBuffType' is referenced directly or indirectly in its own type annotation.

typings/globals/node/index.d.ts(518,26): error TS2300: Duplicate identifier 'Buffer'.

typings/globals/node/index.d.ts(518,50): error TS2300: Duplicate identifier 'SlowBuffer'.

typings/globals/node/index.d.ts(580,22): error TS2320: Interface 'Server' cannot simultaneously extend types 'EventEmitter' and 'Server'.

Named property 'addListener' of types 'EventEmitter' and 'Server' are not identical.

typings/globals/node/index.d.ts(580,22): error TS2320: Interface 'Server' cannot simultaneously extend types 'EventEmitter' and 'Server'.

Named property 'emit' of types 'EventEmitter' and 'Server' are not identical.

typings/globals/node/index.d.ts(580,22): error TS2320: Interface 'Server' cannot simultaneously extend types 'EventEmitter' and 'Server'.

Named property 'on' of types 'EventEmitter' and 'Server' are not identical.

typings/globals/node/index.d.ts(580,22): error TS2320: Interface 'Server' cannot simultaneously extend types 'EventEmitter' and 'Server'.

Named property 'once' of types 'EventEmitter' and 'Server' are not identical.

typings/globals/node/index.d.ts(580,22): error TS2320: Interface 'Server' cannot simultaneously extend types 'EventEmitter' and 'Server'.

Named property 'prependListener' of types 'EventEmitter' and 'Server' are not identical.

typings/globals/node/index.d.ts(580,22): error TS2320: Interface 'Server' cannot simultaneously extend types 'EventEmitter' and 'Server'.

Named property 'prependOnceListener' of types 'EventEmitter' and 'Server' are not identical.

typings/globals/node/index.d.ts(580,22): error TS2320: Interface 'Server' cannot simultaneously extend types 'EventEmitter' and 'Server'.

Named property 'removeListener' of types 'EventEmitter' and 'Server' are not identical.

typings/globals/node/index.d.ts(591,22): error TS2320: Interface 'ServerResponse' cannot simultaneously extend types 'EventEmitter' and 'Writable'.

Named property 'addListener' of types 'EventEmitter' and 'Writable' are not identical.

typings/globals/node/index.d.ts(591,22): error TS2320: Interface 'ServerResponse' cannot simultaneously extend types 'EventEmitter' and 'Writable'.

Named property 'emit' of types 'EventEmitter' and 'Writable' are not identical.

typings/globals/node/index.d.ts(591,22): error TS2320: Interface 'ServerResponse' cannot simultaneously extend types 'EventEmitter' and 'Writable'.

Named property 'on' of types 'EventEmitter' and 'Writable' are not identical.

typings/globals/node/index.d.ts(591,22): error TS2320: Interface 'ServerResponse' cannot simultaneously extend types 'EventEmitter' and 'Writable'.

Named property 'once' of types 'EventEmitter' and 'Writable' are not identical.

typings/globals/node/index.d.ts(591,22): error TS2320: Interface 'ServerResponse' cannot simultaneously extend types 'EventEmitter' and 'Writable'.

Named property 'prependListener' of types 'EventEmitter' and 'Writable' are not identical.

typings/globals/node/index.d.ts(591,22): error TS2320: Interface 'ServerResponse' cannot simultaneously extend types 'EventEmitter' and 'Writable'.

Named property 'prependOnceListener' of types 'EventEmitter' and 'Writable' are not identical.

typings/globals/node/index.d.ts(591,22): error TS2320: Interface 'ServerResponse' cannot simultaneously extend types 'EventEmitter' and 'Writable'.

Named property 'removeListener' of types 'EventEmitter' and 'Writable' are not identical.

typings/globals/node/index.d.ts(621,22): error TS2320: Interface 'ClientRequest' cannot simultaneously extend types 'EventEmitter' and 'Writable'.

Named property 'addListener' of types 'EventEmitter' and 'Writable' are not identical.

typings/globals/node/index.d.ts(621,22): error TS2320: Interface 'ClientRequest' cannot simultaneously extend types 'EventEmitter' and 'Writable'.

Named property 'emit' of types 'EventEmitter' and 'Writable' are not identical.

typings/globals/node/index.d.ts(621,22): error TS2320: Interface 'ClientRequest' cannot simultaneously extend types 'EventEmitter' and 'Writable'.

Named property 'on' of types 'EventEmitter' and 'Writable' are not identical.

typings/globals/node/index.d.ts(621,22): error TS2320: Interface 'ClientRequest' cannot simultaneously extend types 'EventEmitter' and 'Writable'.

Named property 'once' of types 'EventEmitter' and 'Writable' are not identical.

typings/globals/node/index.d.ts(621,22): error TS2320: Interface 'ClientRequest' cannot simultaneously extend types 'EventEmitter' and 'Writable'.

Named property 'prependListener' of types 'EventEmitter' and 'Writable' are not identical.

typings/globals/node/index.d.ts(621,22): error TS2320: Interface 'ClientRequest' cannot simultaneously extend types 'EventEmitter' and 'Writable'.

Named property 'prependOnceListener' of types 'EventEmitter' and 'Writable' are not identical.

typings/globals/node/index.d.ts(621,22): error TS2320: Interface 'ClientRequest' cannot simultaneously extend types 'EventEmitter' and 'Writable'.

Named property 'removeListener' of types 'EventEmitter' and 'Writable' are not identical.

typings/globals/node/index.d.ts(647,22): error TS2320: Interface 'IncomingMessage' cannot simultaneously extend types 'EventEmitter' and 'Readable'.

Named property 'addListener' of types 'EventEmitter' and 'Readable' are not identical.

typings/globals/node/index.d.ts(647,22): error TS2320: Interface 'IncomingMessage' cannot simultaneously extend types 'EventEmitter' and 'Readable'.

Named property 'emit' of types 'EventEmitter' and 'Readable' are not identical.

typings/globals/node/index.d.ts(647,22): error TS2320: Interface 'IncomingMessage' cannot simultaneously extend types 'EventEmitter' and 'Readable'.

Named property 'on' of types 'EventEmitter' and 'Readable' are not identical.

typings/globals/node/index.d.ts(647,22): error TS2320: Interface 'IncomingMessage' cannot simultaneously extend types 'EventEmitter' and 'Readable'.

Named property 'once' of types 'EventEmitter' and 'Readable' are not identical.

typings/globals/node/index.d.ts(647,22): error TS2320: Interface 'IncomingMessage' cannot simultaneously extend types 'EventEmitter' and 'Readable'.

Named property 'prependListener' of types 'EventEmitter' and 'Readable' are not identical.

typings/globals/node/index.d.ts(647,22): error TS2320: Interface 'IncomingMessage' cannot simultaneously extend types 'EventEmitter' and 'Readable'.

Named property 'prependOnceListener' of types 'EventEmitter' and 'Readable' are not identical.

typings/globals/node/index.d.ts(647,22): error TS2320: Interface 'IncomingMessage' cannot simultaneously extend types 'EventEmitter' and 'Readable'.

Named property 'removeListener' of types 'EventEmitter' and 'Readable' are not identical.

typings/globals/node/index.d.ts(698,18): error TS2300: Duplicate identifier 'Agent'.

typings/globals/node/index.d.ts(743,18): error TS2300: Duplicate identifier 'Worker'.

typings/globals/node/index.d.ts(1004,22): error TS2300: Duplicate identifier 'CompleterResult'.

typings/globals/node/index.d.ts(1044,18): error TS2300: Duplicate identifier 'Script'.

typings/globals/node/index.d.ts(1323,22): error TS2320: Interface 'Server' cannot simultaneously extend types 'Socket' and 'EventEmitter'.

Named property 'removeListener' of types 'Socket' and 'EventEmitter' are not identical.

typings/globals/node/index.d.ts(1886,18): error TS2300: Duplicate identifier 'TLSSocket'.

typings/globals/node/index.d.ts(2188,18): error TS2420: Class 'Readable' incorrectly implements interface 'ReadableStream'.

Types of property 'pause' are incompatible. Type '() => Readable' is not assignable to type '{ (): ReadableStream; (): ReadableStream; }'. Type 'Readable' is not assignable to type 'ReadableStream'. Property 'isPaused' is missing in type 'Readable'. typings/globals/node/index.d.ts(2211,18): error TS2420: Class 'Writable' incorrectly implements interface 'WritableStream'.

Types of property 'eventNames' are incompatible. Type '() => (string | symbol)[]' is not assignable to type '() => string[]'. Type '(string | symbol)[]' is not assignable to type 'string[]'. Type 'string | symbol' is not assignable to type 'string'. Type 'symbol' is not assignable to type 'string'. typings/globals/node/index.d.ts(2229,18): error TS2420: Class 'Duplex' incorrectly implements interface 'ReadWriteStream'.

Types of property 'pause' are incompatible. Type '() => Duplex' is not assignable to type '{ (): ReadWriteStream; (): ReadWriteStream; }'. Type 'Duplex' is not assignable to type 'ReadWriteStream'. Property 'isPaused' is missing in type 'Duplex'. typings/globals/node/index.d.ts(2250,18): error TS2420: Class 'Transform' incorrectly implements interface 'ReadWriteStream'.

Types of property 'pause' are incompatible. Type '() => Transform' is not assignable to type '{ (): ReadWriteStream; (): ReadWriteStream; }'. Type 'Transform' is not assignable to type 'ReadWriteStream'. Property 'isPaused' is missing in type 'Transform'. typings/globals/node/index.d.ts(2343,5): error TS2300: Duplicate identifier 'export='.

typings/globals/node/index.d.ts(2365,18): error TS2300: Duplicate identifier 'Domain'.

typings/globals/node/index.d.ts(2365,18): error TS2420: Class 'Domain' incorrectly implements interface 'NodeJS.Domain'.

Types of property 'eventNames' are incompatible. Type '() => (string | symbol)[]' is not assignable to type '() => string[]'. Type '(string | symbol)[]' is not assignable to type 'string[]'. typings/globals/node/index.d.ts(2625,5): error TS2300: Duplicate identifier 'export='.

`

卸载打字。 Typescript 类型现在由 npm 在 @types 命名空间下管理:

npm uninstall --save typings

另外,删除 typings 子目录。

要从 Angular 2 更新到 Angular 4,请按照以下步骤操作:

1 - 确保您不使用 extends OnInit,或将 extends 用于任何生命周期事件。而是使用工具 <lifecycle event>.

2 - 使用

将所有依赖项更新到版本 4 和最新的 TypeScript

Mac/Linux

npm install @angular/{animations,common,compiler,compiler-cli,core,forms,http,platform-browser,platform-browser-dynamic,platform-server,router}@4.0.0 typescript@latest --save

Windows

 npm install @angular/common@next @angular/compiler@next @angular/compiler-cli@next @angular/core@next @angular/forms@next @angular/http@next @angular/platform-browser@next @angular/platform-browser-dynamic@next @angular/platform-server@next @angular/router@next @angular/animations@next --save

3 - 如果你在你的应用程序中使用动画,你应该在你的 App NgModule 中从 @angular/platform-browser/animations 导入 BrowserAnimationsModule。

4 - 将模板标签重命名为 ng-template。

5 - 如果你想验证你的表单添加 ngNativeValidate 因为 angular 4 向所有表单添加了 novalidate 属性


如果您仍然遇到一些问题,请尝试更新 nodejs 和 Typescript npm install -g typescript@latest

如果 none 有帮助,请将您的 Angular CLI 更新到最新版本

npm uninstall -g @angular/cli
npm cache clean
npm install -g @angular/cli@latest

并使用 ng new newProject 生成一个新的 Angular 4 项目,并将您的逻辑和模板移至新应用。