gpt4 book ai didi

angular - Firebase 服务不会产生与 ng 服务相同的结果

转载 作者:太空狗 更新时间:2023-10-29 18:25:50 26 4
gpt4 key购买 nike

我是网络开发的新手,正在尝试制作一个 firebase 托管的 Angular 网络应用程序。我使用 angular-cli 从 Webstorm 生成这个项目。

我有一个简单的 Angular 网络应用程序,它在加载时显示文本

ng serve

它工作正常: working result

然而,当我加载完全相同的代码时

firebase serve

我明白了: not working result

据我所知,firebase 服务存在一些问题,无法连接来自 app.component.ts 的组件。到 html 标签 <app-root>在我的代码中。除此之外,我不知道发生了什么,并花了几天时间试图弄明白。

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

<!-- AngularJS -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.0/angular.min.js"></script>

<!-- AngularFire -->
<script src="https://cdn.firebase.com/libs/angularfire/2.2.0/angularfire.min.js"></script>

</head>
<body>
<app-root>Loading...</app-root>
</body>

<script src="https://www.gstatic.com/firebasejs/3.6.7/firebase.js"></script>
<script>
// Initialize Firebase
var config = {
apiKey: "---",
authDomain: "---",
databaseURL: "---",
storageBucket: "---",
messagingSenderId: "---"
};
firebase.initializeApp(config);
</script>

<script>
const myApp = angular.module("myApp", ['firebase']);
</script>

</html>

app.component.ts

import { Component } from '@angular/core';
import {AngularFire, AuthProviders, AuthMethods, FirebaseListObservable} from 'angularfire2'

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})

export class AppComponent {
title = 'app works!';

items: FirebaseListObservable<any>;

constructor(public af: AngularFire) {}
}

app.component.html

<h1>
{{title}}
</h1>

ma​​in.ts

import './polyfills.ts';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment.prod';
import { AppModule } from './app/app.module';
import { FIREBASE_PROVIDERS, defaultFirebase } from 'angularfire2';

if (environment.production) {
enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule, [
FIREBASE_PROVIDERS,
defaultFirebase('https://<NAME-OF-MY-APP>.firebaseio.com')
]);

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';
import { AngularFireModule } from 'angularfire2';

// Initialize Firebase
export const firebaseConfig = {
apiKey: "---",
authDomain: "---",
databaseURL: "---",
storageBucket: "---",
messagingSenderId: "---"
};

@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
AngularFireModule.initializeApp(firebaseConfig)
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

最佳答案

我想通了...

我一直在部署我的 src 文件夹而不是生成的 dist 文件夹。

对于没有意识到需要部署其 dist 文件夹的任何人,请运行 ng build 来生成它。然后无论您部署站点如何,请上传 dist 文件夹。

如果使用 firebase 进行部署,请将您的 firebase.json “hosting” 标记设置为:

{
"database": {
"rules": "database.rules.json"
},
"hosting": {
"public": "dist"
}
}

然后 Firebase 将始终部署您的 dist 文件夹,只要确保每次您进行更改并想要部署该更改时运行 ng build

如果您使用的是来自 Intellij 的 WebStorm 并且找不到您的 dist 文件夹,请在 Project 工具窗口中,切换到下拉列表中的“Project”选项。

关于angular - Firebase 服务不会产生与 ng 服务相同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41843176/

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