【问题标题】:Angular RC4, One or more of providers for "App" were not defined: [?]Angular RC4,“App”的一个或多个提供者未定义:[?]
【发布时间】:2016-07-22 18:45:35
【问题描述】:

将我的应用程序从 angular2 更新到 RC4 后,我收到了这个错误。我在新的webpack starter 捆绑包中将LoginService 注入AppComponent

我试图在路由中包含 AppComponent,但它不起作用。 我还在 index.ts 中包含了LoginService 和 APP_PROVIDERS,但我遇到了另一个错误

提供者无效 - 只允许提供 Provider 和 Type 的实例, 得到:未定义

还将LoginService 放入引导程序,但没有效果。

RC4 中的 Angular2 有什么问题?组件不在树中了?

这是我的路由器“3.0.0-beta.2”的路由器配置:

export const routes: RouterConfig = [
  { path: '', component: StartPageComponent },
  { path: 'login', component: StartPageComponent },
  {
    path: 'dashboard', component: 'DashboardComponent',
    canActivate: [WebpackAsyncRoute],
    children: [
      { path: '', component: 'ContentDesktopComponent' }  // must be included
    ]
  },
  { path: '**', component: NoContent }

];

我希望每条路线都可以使用 LoginService。

我的 app.component 如下所示:

@Component({
  selector: 'app',
  pipes: [],    
  providers: [LoginService, SearchService, TasksService],   
  directives: [RouterActive],
  encapsulation: ViewEncapsulation.None,
  styles: [
    require('!raw!normalize.css'),
    require('!raw!../assets/css/animate.css'),
    require('./app.component.scss')
  ],
  template: require('./app.component.html')
})
export class App {

  constructor(private translate: TranslateService,
    public appState: AppState) {

    Resource.map(ENDPOINTS.API.toString(), 'http://localhost:3002/api');
    let userLang = navigator.language.split('-')[0]; // use navigator lang if available
    userLang = /(fr|en)/gi.test(userLang) ? userLang : 'pl';
    translate.setDefaultLang('en');
    translate.use('pl');

  }

  ngOnInit() {

  }

}

提供者不适用于路由器...现在...在 rc4 中

登录服务:

@Injectable()
export class LoginService {

    private user: User = <User>{
        avatar_url: '/assets/img/avatars/u6.png'
    };

    constructor(private rest: Resource<ENDPOINTS, User, User[]>) {
        this.rest.add(ENDPOINTS.API, 'users');
    }

【问题讨论】:

  • 请发布代码来演示您尝试完成的工作。
  • 包含@Component() 装饰器的App 类看起来如何?

标签: angular angular2-routing angular2-services angular2-router3


【解决方案1】:

您需要在某处提供TranslateServiceAppState,无论是bootstrap(...) 还是@Component(..., providers: [...]

【讨论】:

  • 静止:One or more of providers for "App" were not defined: [?, SearchService, TasksService, TranslateService].
  • 看起来LoginService 没有正确导入。请使用您当前拥有的代码更新您的问题。我不知道你改变了什么。
  • 其奇怪的错误,因为现在我有 4 个提供者:providers: [LoginService, SearchService, TasksService, TranslateService],
  • Okej 问题出在我的扩展 ng2-rest github.com/darekf77/ng2-rest .... 即使 Resource 在提供程序中,我仍然收到此错误(我在引导程序中有资源已经)。我现在的问题是 RC4 中的 Resource 等通用类服务有什么问题吗?
  • 您在哪里以及如何提供Resource
猜你喜欢
  • 1970-01-01
  • 2017-07-16
  • 1970-01-01
  • 2016-10-04
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多