我正在生成https://editor.swagger.io/ Codegen 代理。它在 Angular 10 中给出了以下错误。如何修复?
通用类型“ModuleWithProviders”需要 1 个类型参数。
export class ApiModule {
public static forRoot(configurationFactory: () => Configuration): ModuleWithProviders {
return {
ngModule: ApiModule,
providers: [ { provide: Configuration, useFactory: configurationFactory } ]
};
}
constructor( @Optional() @SkipSelf() parentModule: ApiModule,
@Optional() http: HttpClient) {
if (parentModule) {
throw new Error('ApiModule is already loaded. Import in your base AppModule only.');
}
if (!http) {
throw new Error('You need to import the HttpClientModule in your AppModule! \n' +
'See also …Run Code Online (Sandbox Code Playgroud) 我正在尝试将项目中的 Angular 从 10.2 版更新到 11.0 版。运行 ng 更新:
@angular-devkit/build-angular 0.1002.0 -> 0.1100.1 ng update @angular-devkit/build-angular
@angular/cdk 10.2.7 -> 11.0.0 ng update @angular/cdk
@angular/cli 10.2.0 -> 11.0.1 ng update @angular/cli
@angular/core 10.2.3 -> 11.0.0 ng update @angular/core
@angular/material 10.2.7 -> 11.0.0 ng update @angular/material
Run Code Online (Sandbox Code Playgroud)
不幸的是,我尝试更新的每个包都失败了。我尝试使用 --force 和 --allowDirty 标志。
@angular-cli
npm ERR! Found: @angular-devkit/build-angular@0.1002.0
npm ERR! node_modules/@angular-devkit/build-angular
npm ERR! dev @angular-devkit/build-angular@"~0.1100.1" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! dev @angular-devkit/build-angular@"~0.1100.1" from the root …Run Code Online (Sandbox Code Playgroud) 目前,我的应用程序已部署,所有 js 都有时间戳版本。现在,当我再次构建我的 angular 应用程序时,js 将有一个新的时间戳版本。
有没有一种方法可以比较我的构建版本,如果不匹配,则允许用户从应用程序注销?
我们目前正在将项目从 Angular 8 升级到 10,并且在构建过程中的文件替换方面遇到了一些麻烦。我们有两种不同的样式和不同的文本,过去是通过在配置中执行 FileReplacements 来选择的。它们看起来像这样:
"fileReplacements": [
{
"replace": "src/index.html",
"with": "src/index.xy.html"
}
{
"replace": "src/assets/text/de.json",
"with": "src/assets/text/de.xy.json"
}
]
Run Code Online (Sandbox Code Playgroud)
奇怪的是,对于 html 文件,替换似乎有效,但对于资产则不然。我们很想让它发挥作用,并且也大致了解为什么它过去有效而现在无效。
以下是我们的 package.json 中的版本:
"dependencies": {
"@angular/common": "10.2.3",
"@angular/compiler": "10.2.3",
"@angular/core": "10.2.3",
"@angular/forms": "10.2.3",
"@angular/platform-browser": "10.2.3",
"@angular/platform-browser-dynamic": "10.2.3",
"@angular/router": "10.2.3",
...
}
"devDependencies": {
"@angular-devkit/build-angular": "~0.1002.0",
"@angular-devkit/build-ng-packagr": "~0.1002.0",
"@angular-devkit/core": "~10.2.0",
"@angular/cli": "^10.2.0",
"@angular/compiler-cli": "^10.2.3",
...
}
Run Code Online (Sandbox Code Playgroud) 我有一个像这样的路由组件:
从组件 A:我有一个通过 API 获取的对象,目前我有一个按钮将我重定向到组件 B。
组件A和B之间没有父子关系。
我想要做的是在重定向到组件 B 的同时发送该对象,这样我就可以对其进行处理。
我不想像这样通过 URL 传递数据:
return this.router.navigate(['associate-product/' + this.id, {this.data}]);
Run Code Online (Sandbox Code Playgroud)
另外我不想将对象存储在 LocalStorage 上。
有办法实现这一点吗?
我想在 TAG Body 的两个类别(浅色和深色)之间切换。
我做了什么?我创建了一个服务:
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ThemeService {
body = document.body;
constructor() { }
changeLight() {
this.body.classList.replace('light', 'dark');
}
changeDark() {
this.body.classList.replace('dark', 'light');
}
}
Run Code Online (Sandbox Code Playgroud)
它按预期工作,但我知道这段代码没有使用最佳实践。
在这两个类之间进行更改的正确方法是什么?
我有一个带有延迟加载模块和路由的普通 Angular 10 应用程序。但是,我有一个特殊的要求需要满足。
在大多数页面上,我想通过<app-root>在 index.html 中嵌入元素来使用路由等初始化完整的应用程序——这是AppComponent. 另一方面,在某些页面上,不应该初始化完整的应用程序,而应该只初始化一个<header-search>我使用 @angular/elements(Web 组件)注册的特定组件。这也意味着在这种情况下,除了<header-search>初始化(如果它不是由<header-search>组件本身嵌入)之外,不应该发生路由,也不应该发生任何其他组件。
旁注只是为了让您了解用例的背景:在我正在构建的项目中,并非所有部分都与 Angular 解耦。一些页面使用 Twig/PHP 在后端呈现。但是我需要使用 Angular 构建的标题中的搜索功能也可以在这些页面上使用。这意味着我不会同时拥有完整的应用程序,只有HeaderSearchComponent在这种情况下。然而,在其他页面上,完整的应用程序将被初始化,包括HeaderSearchComponet,因此不需要使用 Web 组件单独嵌入 - 在这种情况下,
<app-root>元素就足够了。
我的想法在哪里使用角度元素将HeaderSearchComponent作为<header-search>自定义 Web 组件注册,例如:
@NgModule({
imports: [BrowserModule, FormsModule, SharedModule],
declarations: [AppComponent],
bootstrap: [AppComponent],
entryComponents: [HeaderSearchComponent]
})
export class AppModule implements DoBootstrap {
constructor(injector: Injector) {
const webComponent = createCustomElement(HeaderSearchComponent, {
injector
});
customElements.define("header-search", webComponent);
}
public ngDoBootstrap(): void {}
} …Run Code Online (Sandbox Code Playgroud) 在我的图书馆中,我有一个带有以下代码的服务:
import { Inject, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { DataInjectorModule } from '../../data-injector.module';
// @dynamic
@Injectable()
export class RemoteDataService<T> {
@Inject('env') private environment: any = {};
public type: new () => T;
constructor(
model: T,
) {
this.http = DataInjectorModule.InjectorInstance.get(HttpClient);
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
(现有原因data-injector.module只是避免循环依赖):
import { NgModule, Injector } from '@angular/core';
// @dynamic
@NgModule({
declarations: [],
imports: [],
providers: [],
exports: [],
})
export class DataInjectorModule {
static InjectorInstance: Injector; …Run Code Online (Sandbox Code Playgroud) 我已将 MetaMask 与 Angular 集成,在从代码中打开 MetaMask 扩展时出现此错误。
以下是我打开扩展的代码。我在单击打开 MetaMask 时调用此函数,它给出了该错误
MetaMask - RPC 错误:权限请求已挂起;请稍等。{code:-32002, message:“权限请求已待处理;请稍候。”}
ethEnabled(template?): any
{
if ((window as any).web3)
{
(window as any).web3 = new Web3((window as any).web3.currentProvider);
(window as any).ethereum.enable();
console.log((window as any).ethereum.enable());
}
else
{
this.modalRef = this.modalService.show(template);
}
}
Run Code Online (Sandbox Code Playgroud) 在我的 Angular 11 项目中,我在构建和服务项目时遇到以下错误。
(节点:15218)UnhandledPromiseRejectionWarning:错误:路径/ Y /node_modules/ @abc/X /lib/language-translations.d.ts 中声明的符号 LanguageTranslationModule 未从@abc/X导出(导入到路径/ Y /src/ lib/.module.ts) ......
错误:./src/main.ts 模块构建失败(来自 ./node_modules/@ngtools/webpack/src/ivy/index.js):错误:在路径/ Y /node_modules/ @abc/X /lib中声明的符号 LanguageTranslationModule /language-translations.d.ts 未从@abc/X导出(导入到路径/ Y /src/lib/.module.ts).......
错误:./src/polyfills.ts 模块构建失败(来自 ./node_modules/@ngtools/webpack/src/ivy/index.js):错误:在路径/ Y /node_modules/ @abc/X /lib中声明的符号 LanguageTranslationModule /language-translations.d.ts 未从@abc/X导出(导入到路径/ Y /src/lib/.module.ts).......
场景:我想使用一个库(X)作为另一个库(Y)内的依赖项。当我安装Y库中的所有包和依赖包(包括X的)时,它给出了上述错误。
删除节点模块等并重新安装它们对我来说不起作用。
请提出解决此问题的方法。
版本:
Angular CLI: 11.1.4
Node: 10.16.0
OS: linux x64
Angular: 11.1.0
Run Code Online (Sandbox Code Playgroud)
X 库的 package.json 示例:
{
"name": "@abc/X",
"version": "0.0.2",
"peerDependencies": {
"@angular/common": "^11.1.2",
"@angular/core": …Run Code Online (Sandbox Code Playgroud) angular ×10
angular10 ×10
typescript ×2
angular11 ×1
css ×1
html ×1
injectable ×1
javascript ×1
metamask ×1
node.js ×1
swagger ×1
webpack ×1