Angular 5: 'flash-messages' is not a known element

心已入冬 提交于 2020-01-06 05:30:22

问题


I'm trying to use the Angular flash message module as shown in this post: https://www.npmjs.com/package/angular-flash-message

However, I get this error:

> compiler.js:485 Uncaught Error: Template parse errors:
> 'flash-messages' is not a known element:
> 
> 1. If 'flash-messages' is an Angular component, then verify that it is part of this module.
> 2. If 'flash-messages' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component
> to suppress this message. ("<app-navbar></app-navbar><div
> class="container">[ERROR
> ->]<flash-messages></flash-messages><router-outlet></router-outlet> </div>"): ng:///AppModule/AppComponent.html@2:8

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { Routes, RouterModule } from '@angular/router';
import { AppRoutingModule } from './app-routing.module';

import { AppComponent } from './app.component';
import { NavbarComponent } from './components/navbar/navbar.component';
import { LoginComponent } from './components/login/login.component';
import { HomeComponent } from './components/home/home.component';
import { DashboardComponent } from './components/dashboard/dashboard.component';
import { ProfileComponent } from './components/profile/profile.component';
import { RegisterComponent } from './components/register/register.component';

import { ValidateService } from './services/validate.service';
import {FlashMessageModule} from 'angular-flash-message'

@NgModule({
  declarations: [
    AppComponent,
    NavbarComponent,
    LoginComponent,
    HomeComponent,
    DashboardComponent,
    ProfileComponent,
    RegisterComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    RouterModule,
    FormsModule,
    FlashMessageModule
  ],
  providers: [ValidateService],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.html

<app-navbar></app-navbar>
<div class="container">
        <flash-messages></flash-messages>
        <router-outlet></router-outlet>
</div>

registration.component.ts

import { Component, OnInit } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { ValidateService } from '../../services/validate.service';
import { FlashMessage } from 'angular-flash-message';

@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.scss']
})
export class RegisterComponent implements OnInit {

  name: String;
  username: String;
  email: String;
  password: String;

  constructor(
    private validateService: ValidateService,
    private flashMessage: FlashMessage
  ) { }

  ngOnInit() {
  }

  onRegisterSubmit(){
    const user = {
      name: this.name,
      username: this.username,
      email: this.email,
      password: this.password
    }

    //Required fields
    if(!this.validateService.validateRegister(user)){
      //fill in all fields
      this.flashMessage.info("Incorrect!")
      return false;
    }
    
    if(!this.validateService.validateEmail(user.email)){

      //email incorrect
      return false;
    }

  }

}

I'm trying to display a flash message after validating the user's input. I'm new to Angular and MEAN apps and this is actually an app shown in a tutorial using Angular 2.

Can someone help me out? Thanks!


回答1:


I'm not sure if the

import {FlashMessageModule} from 'angular-flash-message'

works properly. I would suggest using

import {FlashMessageModule} from 'angular2-flash-messages'`.

Their documentation is here: https://www.npmjs.com/package/angular2-flash-messages

I went through the angular-flash messages docs, angular2-flash-message docs (without the 's' at the end), and also angular2-flash-messages (with the 's' at the end), and the only one that worked for me was the last one.




回答2:


Also, in addition to your imports, I've also found that it helps to make sure you have @angular/common installed, just in case:

npm install @angular/common --save

Hope this helps




回答3:


ooh i think it shouldn't be

ERRONEOUS

imports: [ BrowserModule, FormsModule, RouterModule.forRoot(appRoutes), AngularFireModule.initializeApp(fireBase), FlashMessagesModule ],

it should be

imports: [ BrowserModule, FormsModule, RouterModule.forRoot(appRoutes), AngularFireModule.initializeApp(fireBase), FlashMessagesModule.forRoot() ],

I too had the same problem and solved it by adding FlashMessageModule.forRoot() in my imports



来源:https://stackoverflow.com/questions/48477330/angular-5-flash-messages-is-not-a-known-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!