gpt4 book ai didi

javascript - Angular 静态注入(inject)器错误

转载 作者:行者123 更新时间:2023-12-03 01:42:25 24 4
gpt4 key购买 nike

您好,我是 Angular 的新手,所以我按照 YouTube 教程来构建聊天。本项目在业主github上->https://github.com/wesdoyle/base-chat

我尝试将 AngularFireDatabase 添加到提供程序,但它仍然无法正常工作。我不知道为什么当我尝试登录时会收到此错误。 error login

这是我的 app.module.ts 文件:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import { AngularFireModule } from 'angularfire2';
import { AngularFireDatabaseModule } from 'angularfire2/database';
import { AngularFireAuthModule } from 'angularfire2/auth';



import { ChatFormComponent } from './chat-form/chat-form.component';
import { ChatroomComponent } from './chatroom/chatroom.component';
import { FeedComponent } from './feed/feed.component';
import { MessageComponent } from './message/message.component';
import { LoginFormComponent } from './login-form/login-form.component';
import { SignupFormComponent } from './signup-form/signup-form.component';
import { NavbarComponent } from './navbar/navbar.component';
import { UserListComponent } from './user-list/user-list.component';
import { UserItemComponent } from './user-item/user-item.component';

import { ChatService } from './services/chat.service';
import { AuthService } from './services/auth.service';

import { appRoutes } from '../routes';
import { environment } from '../environments/environment';


@NgModule({
declarations: [
AppComponent,
ChatFormComponent,
ChatroomComponent,
FeedComponent,
MessageComponent,
LoginFormComponent,
SignupFormComponent,
NavbarComponent,
UserListComponent,
UserItemComponent
],
imports: [
BrowserModule,
RouterModule.forRoot(appRoutes),
FormsModule,
AngularFireModule,
AngularFireDatabaseModule,
AngularFireAuthModule,
AngularFireModule.initializeApp(environment.firebase)
],
providers: [AuthService,ChatService],
bootstrap: [AppComponent]
})
export class AppModule { }

这就是我抛出错误的login-form.component.ts

import { Component, OnInit } from '@angular/core';
import { AuthService } from '../services/auth.service';
import { AngularFireDatabase} from 'angularfire2/database';
import { Router } from '@angular/router';


@Component({
selector: 'app-login-form',
templateUrl: './login-form.component.html',
styleUrls: ['./login-form.component.css']
})
export class LoginFormComponent {
email: string;
password: string;
errorMsg: string;

constructor(private authService: AuthService, private router: Router) { }

login() {
console.log('login() called from login-form component');
this.authService.login(this.email, this.password)
.catch(error => this.errorMsg = error.message);
}
}

这是chat.service.ts:

import { Injectable } from '@angular/core';
import { AngularFireModule } from 'angularfire2';
import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database-deprecated';
import { AngularFireAuth } from 'angularfire2/auth';
import { Observable,of } from 'rxjs';
import { AuthService } from '../services/auth.service';
import * as firebase from 'firebase/app';

import { ChatMessage } from '../models/chat-message.model';

@Injectable()
export class ChatService {
user: firebase.User;
chatMessages: FirebaseListObservable<ChatMessage[]>;
chatMessage: ChatMessage;
userName: Observable<string>;

constructor(
private db: AngularFireDatabase,
private afAuth: AngularFireAuth
) {
this.afAuth.authState.subscribe(auth => {
if (auth !== undefined && auth !== null) {
this.user = auth;
}

this.getUser().subscribe(a => {
this.userName = a.displayName;
});
});
}

getUser() {
const userId = this.user.uid;
const path = `/users/${userId}`;
return this.db.object(path);
}

getUsers() {
const path = '/users';
return this.db.list(path);
}

sendMessage(msg: string) {
const timestamp = this.getTimeStamp();
const email = this.user.email;
this.chatMessages = this.getMessages();
this.chatMessages.push({
message: msg,
timeSent: timestamp,
userName: this.userName,
email: email });
}

getMessages(): FirebaseListObservable<ChatMessage[]> {
// query to create our message feed binding
return this.db.list('messages', {
query: {
limitToLast: 25,
orderByKey: true
}
});
}

getTimeStamp() {
const now = new Date();
const date = now.getUTCFullYear() + '/' +
(now.getUTCMonth() + 1) + '/' +
now.getUTCDate();
const time = now.getUTCHours() + ':' +
now.getUTCMinutes() + ':' +
now.getUTCSeconds();

return (date + ' ' + time);
}
}

最佳答案

在您的 Chart.service.ts 中,您有一个错误的导入,该导入已折旧,

改变

来自

import { AngularFireDatabase, FirebaseListObservable, FirebaseObjectObservable } from 'angularfire2/database-deprecated';

import { AngularFireDatabase,FirebaseListObservable,FirebaseObjectObservable } from 'angularfire2/database';

关于javascript - Angular 静态注入(inject)器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50776628/

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