gpt4 book ai didi

angularjs - 如何以 Angular 将依赖项注入(inject)到 module.config(configFn) 中

转载 作者:行者123 更新时间:2023-12-03 05:20:21 25 4
gpt4 key购买 nike

在 Angular 中,我们可以将 $routeProvider 注入(inject)到 config 函数

module.config(function ($routeProvider) {


});

我想将我的服务注入(inject)其中

module.config(function ($routeProvider, myService) {


});

我确信该服务已正确定义,但它会抛出异常,指出未知的 myService,当我注入(inject)时发生事件

module.config(function ($routeProvider, $http) {


});

它仍然显示未知$http

你知道为什么吗?

最佳答案

来自Modules页面,“模块加载和依赖项”部分:

Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.

Run blocks - get executed after the injector is created and are used to kickstart the application. Only instances and constants can be injected into run blocks. This is to prevent further system configuration during application run time.

所以你不能将自己的服务或内置服务(如 $http)注入(inject)到 config() 中。使用run()相反。

关于angularjs - 如何以 Angular 将依赖项注入(inject)到 module.config(configFn) 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15286588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com