angular.js - angular2 Cannot resolve all parameters for ’Parser’(?).

浏览:35日期:2022-12-20

问题描述

刚搭建一个angular2的框架,angular版本2.4.0,webpack打包完浏览器console报错:bundle.min.js:39162 Uncaught Error: Cannot resolve all parameters for ’Parser’(?). Make sure that all the parameters are decorated with Inject or have valid type annotations and that ’Parser’ is decorated with Injectable.但是angular版本改为2.0的就不会报错,代码也是最简单的官网上的demo,以下是tsconfig.json配置

{ 'compilerOptions': { 'target': 'es5', 'module': 'commonjs', 'moduleResolution': 'node', 'sourceMap': true, 'emitDecoratorMetadata': true, 'experimentalDecorators': true, 'removeComments': false, 'noImplicitAny': false }, 'exclude': [ 'node_modules', 'typings/main', 'typings/main.d.ts' ], 'compileOnSave': false, 'buildOnSave': false}

是ts配置出错还是什么原因呢,求解

问题解答

回答1:

从你的报错看应该是DI出了问题,可以试试把你的target改为:

'target': 'es6'

不行的话就试试在你的服务里显示地引入操作符:

import {Inject} from ’@angular/core’;//...constructor(@Inject(SomeService) someService: SomeService);

希望你能顺利解决 :)

回答2:

由于你没有贴完整的代码,我只能提个猜测:应该是你调用某个函数的时候,没有给它传函数所需的参数。举个栗子:

function fn(str){ console.log(str || ’参数未定义’);}fn();//参数不正确

由于typescript会对函数的参数也会严格检查,这段代码在执行fn()的时候,没有给fn传递str参数,所以编译的时候会错误。

你可以借鉴一下别人的项目来搭建angular开发环境,github有非常多的使用webpack搭建angular开发环境的例子,这里有个比较全也比较好的demo可以拿去参考:https://github.com/ntesmail/a...

相关文章: