useFactory vs useValue with AoT compilation

时间:2017-08-07 06:23:32

标签: angular angular-di

我使用window.location设置注射。 在我的接近导入的模块中,我定义了变量

const flag = window.location.search.includes('flag');
...
{ provide: FLAG, useValue: flag },

并且它与JIT编译一样正常工作 但是,当我切换到AoT时,它会中断 useFactory适用于两种情况

export function flagFactory() {
  return window.location.search.includes('flag');;
}
...
{ provide: FLAG, useFactory: flagFactory },

为什么undefined useValuetrue useFactory获得{{1}}?

1 个答案:

答案 0 :(得分:1)

我的猜测是from django.core.management import BaseCommand from multiprocessing import Pool from django.contrib.auth.models import User from pprint import pprint from CentralControl.models import Product, Supplier from CentralControl.management.helpers.map_ingram import * from CentralControl.management.helpers.helper_generic import * from tqdm import tqdm from CentralControl.management.helpers.config import get_ingram import os, sys, csv, zipfile, CentralControl # Run Script as 'SYSTEM' user = User.objects.get(id=1) # Get Connection config. SUPPLIER_CODE, FILE_LOCATION, FILE_NAME = get_ingram() class Command(BaseCommand): def handle(self, *args, **options): list_in = get_file() list_current = get_current_list() pool = Pool(6) pool.map(compare_lists(list_in, list_current)) pool.close() def compare_lists(list_in, list_current): for row_current in tqdm(list_current): for row_in in list_in: if row_in['order_code'] == row_current['order_code']: #do more stuff here. pass def get_current_list(): try: supplier = Supplier.objects.get(code='440040') current_list = Product.objects.filter(supplier=supplier).values() return current_list except: print('Error no products with supplier') exit() def get_file(): with zipfile.ZipFile(FILE_LOCATION + 'incoming/' + FILE_NAME, 'r') as zip: with zip.open('228688 .csv') as csvfile: reader = csv.DictReader(csvfile) list_in = (list(reader)) for row in tqdm(list_in): row['order_code'] = row.pop('Ingram Part Number') row['order_code'] = (row['order_code']).lstrip("0") row['name'] = row.pop('Ingram Part Description') row['description'] = row.pop('Material Long Description') row['mpn'] = row.pop('Vendor Part Number') row['gtin'] = row.pop('EANUPC Code') row['nett_cost'] = row.pop('Customer Price') row['retail_price'] = row.pop('Retail Price') row['qty_at_supplier'] = row.pop('Available Quantity') row['backorder_date'] = row.pop('Backlog ETA') row['backorder_date'] = (row['backorder_date']) row['backorder_qty'] = row.pop('Backlog Information') zip.close() #commented out for dev precess. #os.rename(FILE_LOCATION + 'incoming/' + FILE_NAME, FILE_LOCATION + 'processed/' + FILE_NAME) return list_in 静态分析AoT结构之外的代码。所以它会看到NgModule并提前执行此操作。但是在编译时,这显然会返回window.location.search.includes。在使用undefined的情况下,它不会尝试提前执行主体,仅在运行时执行。

这是AOT的(很多)陷阱之一。始终尝试将每个符号静态分析