@service @component 注入先后顺序_Angular应用的依赖注入调试

上下文:我实现了一个hero service,在其构造函数里添加了一些打印语句:

169d5220f561e4b893c9349d4adab69e.png

在另一个Component的构造函数里将其注入:

db45c00b826a7446cb827949b268abec.png

运行时,观察service构造函数如何被调用的:

646842f7ba01bd348220af704b27bfc8.png
558e20fd43a3f1dbdb73ca8fc00772e8.png
function getNodeInjectable(lView, tView, index, tNode) {    let value = lView[index];    const tData = tView.data;    if (isFactory(value)) {        const factory = value;        if (factory.resolving) {            throw new Error(`Circular dep for ${stringifyForError(tData[index])}`);        }        const previousIncludeViewProviders = setIncludeViewProviders(factory.canSeeViewProviders);        factory.resolving = true;        let previousInjectImplementation;        if (factory.injectImpl) {            previousInjectImplementation = setInjectImplementation(factory.injectImpl);        }        enterDI(lView, tNode);        try {            value = lView[index] = factory.factory(undefined, tData, lView, tNode);            // This code path is hit for both directives and providers.            // For perf reasons, we want to avoid searching for hooks on providers.            // It does no harm to try (the hooks just won't exist), but the extra            // checks are unnecessary and this is a hot path. So we check to see            // if the index of the dependency is in the directive range for this            // tNode. If it's not, we know it's a provider and skip hook registration.            if (tView.firstCreatePass && index >= tNode.directiveStart) {                ngDevMode && assertDirectiveDef(tData[index]);                registerPreOrderHooks(index, tData[index], tView);            }        }        finally {            if (factory.injectImpl)                setInjectImplementation(previousInjectImplementation);            setIncludeViewProviders(previousIncludeViewProviders);            factory.resolving = false;            leaveDI();        }    }    return value;}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值