【问题标题】:Unexpected value ' ' imported by the module ' '. Please add a @NgModule annotation模块“ ”导入了非预期值“ ”。请添加@NgModule 注释
【发布时间】:2018-06-26 15:41:20
【问题描述】:

我正在做这个教程:https://youtu.be/qs2n_poLarc?list=WL 并且正在尝试学习 ionic 框架。

问题是本教程(根据我阅读的内容)有点过时了。视频的作者使用了import { HttpModule } from "@angular/http,但我在 StackOverflow 上看到我应该使用 import { HttpClient } from "@angular/common/http";

问题是当我尝试编译代码时出现此错误:Unexpected value 'HttpClient' imported by the module 'AppModule'. Please add a @NgModule annotation.。现在我不知道应该在哪里添加它,因为我的app.module.ts 看起来像这样:

import { NgModule, ErrorHandler } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { IonicApp, IonicModule, IonicErrorHandler } from "ionic-angular";
import { MyApp } from "./app.component";
import { HttpClient } from "@angular/common/http";

import { AboutPage } from "../pages/about/about";
import { ContactPage } from "../pages/contact/contact";
import { HomePage } from "../pages/home/home";
import { TabsPage } from "../pages/tabs/tabs";
import { SettingsPage } from "../pages/settings/settings";

import { StatusBar } from "@ionic-native/status-bar";
import { SplashScreen } from "@ionic-native/splash-screen";
import { WeatherProvider } from "../providers/weather/weather";

@NgModule({
  declarations: [
    MyApp,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage,
    SettingsPage
  ],
  imports: [BrowserModule, IonicModule.forRoot(MyApp), HttpClient], //Added it right here
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage,
    SettingsPage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    { provide: ErrorHandler, useClass: IonicErrorHandler },
    WeatherProvider,
    HttpClient
  ]
})
export class AppModule {}

知道我在这里缺少什么吗?我找到了this 的答案,但在那里找不到解决方案。

【问题讨论】:

  • 答案有帮助吗?

标签: javascript angularjs typescript ionic-framework


【解决方案1】:

应该是HttpClientModule,改,

来自

imports: [BrowserModule, IonicModule.forRoot(MyApp), HttpClient],

imports: [BrowserModule, IonicModule.forRoot(MyApp), HttpClientModule],

确保您已添加

import { HttpClientModule, HttpClient } from '@angular/common/http';

【讨论】:

    【解决方案2】:

    这意味着它不将其识别为模块。试试这个:

    import {HttpClientModule} from '@angular/common/http';
    

    【讨论】:

    • 所以只是名字的问题?
    • @alex3wielk - 是的,看起来 Angular 团队重命名了模块。我检查了文档。想知道为什么有人投了反对票……:|
    • :哦,刚才我意识到是错误的!我是用手机做的!我实际上支持你,因为你添加了正确的导入部分。我诚挚的歉意。因为我不能再次投票,所以我会投票给你的其他答案之一
    【解决方案3】:

    您需要将 HttpClient 替换为 HttpClientModule 并且不要更改导入部分。 因为 HttpClientModule 访问 HttpClient 的很多部分

    【讨论】:

      猜你喜欢
      • 2018-04-16
      • 2018-08-24
      • 2017-10-23
      • 1970-01-01
      • 2021-10-07
      • 2018-01-10
      • 2018-05-24
      • 2019-12-01
      • 2021-09-02
      相关资源
      最近更新 更多