AoT编译的标记化 [英] Tokenizing for AoT compilation

查看:136
本文介绍了AoT编译的标记化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直对 JIT之间的不一致有一些疑问和AoT编译器。最近困扰我的错误是错误:无法解析IndexedDBCache的所有参数。 IndexedDBCache是​​一种依赖于 string 参数的服务:



请注意,当我删除受保护属性!

  // indexeddb-cache.ts 
从 @ angular / core导入{可注入};

@ Injectable()
导出类IndexedDBCache {
构造函数(受保护的数据库名称:字符串){}
}

我正在使用一家工厂提供该服务的版本:

  // test-api.cache.factory.ts 
从'../indexeddb-cache/indexeddb-cache'导入{IndexedDBCache};

导出函数testIndexedDBCacheFactory(){返回新的IndexedDBCache('test'); }

// test-api.cache.ts
从 @ angular / core导入{InjectionToken,Provider};
从 ../indexeddb-cache/indexeddb-cache导入{IndexedDBCache};
从 ./test-api.cache.factory导入{testIndexedDBCacheFactory;

export const testIndexedDBCache = new InjectionToken< IndexedDBCache>('testIndexedDBCache');

导出let testIndexedDBCacheProvider:提供者= {
提供:testIndexedDBCache,
useFactory:testIndexedDBCacheFactory
};

注意:这些文件必须根据 func-in-providers-useFactory arrow-function-exports -不要问我为什么= /



现在,AoT编译器根本不喜欢此 string 参数。我已经调查了这个问题,但只能找到对 OpaqueToken 的引用(现在已被描述并替换为 InjectionToken< string> )。我的代码现在显示为:

  // test-api.cache.factory.ts 
从 @ angular / core导入{InjectionToken};
从 ../indexeddb-cache/indexeddb-cache导入{IndexedDBCache};

const testIndexedDBCacheFactoryToken = new InjectionToken< string>( test);
导出函数testIndexedDBCacheFactory(){返回新的IndexedDBCache(testIndexedDBCacheFactoryToken); }

显然,由于构造函数只允许使用字符串,因此无法编译参数。我没有足够的有关InjectionTokens或AoT问题的知识来解决此问题-任何人都建议一个可行的结构?



我的代码中有更多上下文并可以在 angular / angular#17295 中找到问题。。 p>




我尝试过的事情:


  1. 删除受保护的访问修饰符> 仍然存在完全相同的错误

  2. 删除字符串参数> 不是一个选项-它定义了服务

  3. 在工厂中用 InjectionToken< string> 替换字符串参数> InjectionToken不是合适的参数


解决方案

对眼前问题的误解。要分解的类本身不是服务,因此不需要 @Injectable 属性。有关详细信息,请参见创建与AoT兼容的服务工厂


I've been having some issues with the inconsistencies between the JIT and AoT compilers. The most recent error that has stumped me was Error: Can't resolve all parameters for IndexedDBCache. IndexedDBCache is a service that depends on a string parameter:

Please note this issue also arises when I remove the 'protected' property!

// indexeddb-cache.ts
import { Injectable } from '@angular/core';

@Injectable()
export class IndexedDBCache {
  constructor(protected databaseName : string) {}
}

I'm using a factory to provide versions of the service:

// test-api.cache.factory.ts
import { IndexedDBCache } from '../indexeddb-cache/indexeddb-cache';

export function testIndexedDBCacheFactory() { return new IndexedDBCache('test'); }

// test-api.cache.ts
import { InjectionToken, Provider } from '@angular/core';
import { IndexedDBCache } from '../indexeddb-cache/indexeddb-cache';
import { testIndexedDBCacheFactory } from './test-api.cache.factory';

export const testIndexedDBCache = new InjectionToken<IndexedDBCache>('testIndexedDBCache');

export let testIndexedDBCacheProvider : Provider = {
  provide: testIndexedDBCache,
  useFactory: testIndexedDBCacheFactory
};

Note : These files have to be split according to func-in-providers-useFactory and arrow-function-exports - don't ask me why =/

Now the AoT compiler doesn't like this string paramter at all. I've looked into the issue but could only find reference to OpaqueToken (now depricated and replaced by InjectionToken<string>). My code would now read:

// test-api.cache.factory.ts
import { InjectionToken } from '@angular/core';
import { IndexedDBCache } from '../indexeddb-cache/indexeddb-cache';

const testIndexedDBCacheFactoryToken = new InjectionToken<string>('test');
export function testIndexedDBCacheFactory() { return new IndexedDBCache(testIndexedDBCacheFactoryToken); }

Obviously this is not compiling as the constructor would only allow for a string parameter. I don't have enough knowledge about InjectionTokens or the AoT issue at hand to solve this - anyone have a suggestion for a construct that would work?

More context on my code and issue to be found at angular/angular#17295.


Things I've tried:

  1. Removing the protected access modifier > Exact same error persists
  2. Removing the string parameter > Not an option - it defines the service
  3. Replacing the string parameter in the factory with an InjectionToken<string> > An InjectionToken is not a suitable parameter

解决方案

There was a misunderstanding of the issue at hand. The classes to be factoried are not services themselves and as such do not need the @Injectable property. More detail to be found at Creating AoT compatible Service Factories

这篇关于AoT编译的标记化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆