gpt4 book ai didi

angular - 错误: Error encountered resolving symbol values statically. 只能引用初始化的变量和常量

转载 作者:太空狗 更新时间:2023-10-29 17:51:45 24 4
gpt4 key购买 nike

我遇到了问题,不知道该怎么办>当我键入 ng serve 时,出现以下错误

ERROR in Error: Error encountered resolving symbol values statically. Only initialized variables and constants can be referenced because the value of this variable is needed by the template compiler (position 175:22 in the original .ts file), resolving symbol NgModule in /Users/Frontend/node_modules/@angular/core/src/metadata/ng_module.d.ts, resolving symbol CoreModule in /Users/Frontend/src/app/core/core.module.ts, resolving symbol CoreModule in /Users/Frontend/src/app/core/core.module.ts at positionalError (/Users/Frontend/node_modules/@angular/compiler/bundles/compiler.umd.js:25273:35) at simplifyInContext (/Users/Frontend/node_modules/@angular/compiler/bundles/compiler.umd.js:25116:27) at StaticReflector.simplify (/Users/Frontend/node_modules/@angular/compiler/bundles/compiler.umd.js:25130:13) at StaticReflector.annotations (/Users/Frontend/node_modules/@angular/compiler/bundles/compiler.umd.js:24558:41) at _getNgModuleMetadata (/Users/Frontend/node_modules/@angular/compiler-cli/src/ngtools_impl.js:138:31) at _extractLazyRoutesFromStaticModule (/Users/Frontend/node_modules/@angular/compiler-cli/src/ngtools_impl.js:109:26) at /Users/Frontend/node_modules/@angular/compiler-cli/src/ngtools_impl.js:129:27 at Array.reduce (native) at _extractLazyRoutesFromStaticModule (/Users/Frontend/node_modules/@angular/compiler-cli/src/ngtools_impl.js:128:10) at Object.listLazyRoutesOfModule (/Users/Frontend/node_modules/@angular/compiler-cli/src/ngtools_impl.js:53:22) at Function.NgTools_InternalApi_NG_2.listLazyRoutes (/Users/Frontend/node_modules/@angular/compiler-cli/src/ngtools_api.js:91:39) at AotPlugin._getLazyRoutesFromNgtools (/Users/Frontend/node_modules/@ngtools/webpack/src/plugin.js:207:44) at _donePromise.Promise.resolve.then.then.then.then.then (/Users/Frontend/node_modules/@ngtools/webpack/src/plugin.js:443:24) at process._tickCallback (internal/process/next_tick.js:109:7)

webpack: Failed to compile.

这是我的CoreModule

import {CommonModule} from '@angular/common';
import {NgModule} from '@angular/core/src/metadata/ng_module';
import {SkipSelf, Optional} from '@angular/core';
import {HttpErrorHandler} from './services/http-error-handler';
import {AuthService} from './services/auth.service';
import {PrivatePageGuard} from './services/private-page.guard';
import {PublicPageGuard} from './services/public-page.guard';
import {XHRBackend, Http, RequestOptions, HttpModule} from '@angular/http';
import {JsonHttp} from './services';

export function createJsonHttp(xhrBackend: XHRBackend, requestOptions:
RequestOptions) {
const ngHttp = new Http(xhrBackend, requestOptions);
return new JsonHttp(ngHttp);
}

@NgModule({
imports: [
CommonModule,
HttpModule
],
exports: [],
providers: [
{
provide: JsonHttp,
useFactory: createJsonHttp,
deps: [XHRBackend, RequestOptions]
},
HttpErrorHandler,
AuthService,
PrivatePageGuard,
PublicPageGuard,
]
})
export class CoreModule {

constructor(@Optional() @SkipSelf() parentModule: CoreModule) {
if (parentModule) {
throw new Error(
'CoreModule is already loaded. Import it in the AppModule only');
}
}

}

这是我的 app.module.ts,我在其中使用了 coremodule

    import {NgModule, ApplicationRef} from "@angular/core";
import {BrowserModule} from "@angular/platform-browser";
import {RouterModule, PreloadAllModules} from "@angular/router";
import {FormsModule, ReactiveFormsModule} from "@angular/forms";
import {ENV_PROVIDERS} from "./environment";
import {ROUTES} from "./app.routes";
import {AppComponent} from "./app.component";
import {CoreModule} from "./core";
import {HomeModule} from "./pages/home/home.module";
import {AuthModule} from "./pages/auth/auth.module";
import {HeaderModule} from "./components/header/header.module";
import {removeNgStyles, createInputTransfer, createNewHosts} from "@angularclass/hmr";
import {TransactionsModule} from "./pages/transactions/transactions.module";

@NgModule({
bootstrap: [AppComponent],
declarations: [
AppComponent
],
imports: [
BrowserModule,
RouterModule.forRoot(ROUTES, {
preloadingStrategy: PreloadAllModules
}),
FormsModule,
ReactiveFormsModule,

CoreModule,
HomeModule,
AuthModule,
HeaderModule,
TransactionsModule
],
providers: [
ENV_PROVIDERS,
]
})
export class AppModule {
constructor(public appRef: ApplicationRef) {
}

hmrOnInit(store) {
if (!store || !store.state) return;
console.log('HMR store', store);
console.log('store.state.data:', store.state.data);
// inject AppStore here and update it
// this.AppStore.update(store.state)
if ('restoreInputValues' in store) {
store.restoreInputValues();
}
// change detection
this.appRef.tick();
delete store.state;
delete store.restoreInputValues;
}

hmrOnDestroy(store) {
const cmpLocation = this.appRef.components.map(cmp => cmp.location.nativeElement);
// recreate elements
store.disposeOldHosts = createNewHosts(cmpLocation);
// inject your AppStore and grab state then set it on store
// var appState = this.AppStore.get()
store.state = {data: 'yolo'};
// store.state = Object.assign({}, appState)
// save input values
store.restoreInputValues = createInputTransfer();
// remove styles
removeNgStyles();
}

hmrAfterDestroy(store) {
// display new elements
store.disposeOldHosts();
delete store.disposeOldHosts;
// anything you need done the component is removed
}

}

至于我的 app.routes.ts :

import {Routes} from "@angular/router";
import {HomeComponent} from "./pages/home/home.component";
import {AuthComponent} from "./pages/auth/auth.component";
import {PrivatePageGuard} from "./core/services/private-page.guard";
import {PublicPageGuard} from "./core/services/public-page.guard";
import {TransactionsComponent} from "./pages/transactions/transactions.component";

export const ROUTES: Routes = [
{
path: 'home',
component: HomeComponent,
canActivate: [PrivatePageGuard]
},
{
path: 'transactions',
component: TransactionsComponent,
canActivate: [PrivatePageGuard]
},
{
path: 'login',
component: AuthComponent,
canActivate: [PublicPageGuard]
},

];

最佳答案

你导入错误:

import {NgModule} from '@angular/core/src/metadata/ng_module';

应该是:

import {NgModule} from '@angular/core';

关于angular - 错误: Error encountered resolving symbol values statically. 只能引用初始化的变量和常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46633809/

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