How to deploy an Angular 2 App using Firebase hosting?

落花浮王杯 提交于 2019-11-29 14:39:49

问题


I want to know the steps necessary to deploy a simple Angular 2 Application using Firebase-hosting.


回答1:


These are the steps:

1) npm install -g firebase-tools 

This will install firebase CLI that we will use in the following steps.

Firebase CLI requires Node.js version 0.10.0 or greater.

2) firebase init

Project setup

This will trigger Firebase project setup and store all settings in a local file firebase.json.

  • ? What Firebase CLI features do you want to setup for this folder? Make sure [Hosting: Configure and deploy Firebase Hosting sites] is checked and press INTRO.

  • ? What Firebase project do you want to associate as default? Choose [create a new project]

Hosting setup

  • ? What do you want to use as your public directory? You need to choose the (build) folder for your Angular 2 Application. Default is (public).
  • ? Configure as a single-page app (rewrite all urls to /index.html)? Answer Yes.

Note: anything under this folder will be served as static assets.

4) You need to go to (https://console.firebase.google.com) to create a new Project.
  • Click on (CREATE NEW PROJECT).

  • Pick up a cool name for your project and select a Country/region. Eg: United Kingdom.

Your project name will look something like cool-f5b0d.

5) firebase use --add

Pick up the project you just created.

  • ? Which project do you want to add? Choose the new project you created.

  • ? What alias do you want to use for this project? You can use an alias for easy reference

6) firebase deploy

This will deploy your asset folder set up during step 2. Make sure this matches the (build) folder for your Angular 2 Application.




回答2:


Below steps shows How to deploy Angular 2 project to firebase hosting:

  1. Build your project, for example in webstorm inside terminal run command:

    pub build

The command will run and create a build/web directory which include your compiled project

  1. In your command line initialize your firebase project by run:

    firebase init

follow the instructions until you reach below question:

What do you want to use as your public directory?
  1. Here you've to give the full path of your project directory build/web starting from home directory. In windows for example the home directory is:

    c:\users\YOU-USER-DIRECTORY

so if your project available in c:\users\YOU-USER-DIRECTORY\projects\MyProject, then give below directory to firebase:

projects/MyProject/build/web

make sure to use front slash / not backslash \

  1. After finishing rest firebase questions, run:

    firebase deploy

it will take some time to upload all project files, then enjoy!




回答3:


Do Create a project with Angular CLI first. Get More info here https://cli.angular.io/

Step 1: Build your App

Run the below cmd to build

ng build --prod

Step 2: Create FireBase project and Install Firebase CLI

Open the Firebase console at https://console.firebase.google.com/ and create a new Firebase project.

To install the Firebase command line tools run:

npm install -g firebase-tools

Step 3: Deploy to FireBase

Run the below firebase cmd to login:

firebase login

It will open the browser and ask you for authentication. Login with your Firebase account. There after you can close the browser window. On the command line you'll receive the message that the login has been performed successfully.

Now run the below cmd:

firebase init

First of all you're being asked which of the Firebase client features you want to use. You should select the option Hosting: Configure and deploy Firebase Hosting site. Next the Firebase client will ask which folder to use for deployment. Type in dist. That is important because that is the location where our production build is stored.

Next the question is ask if this app is a single page app and if it should rewrite all URLs to index.html. In our case we need to answer yes.

Last question is if Firebase should over write file index.html. The answer to that question is no.

Now, Run the below cmd to deploy:

firebase deploy

Firebase will provide a URL which you can use to access your application online.




回答4:


Below is the complete steps which I have followed and hosted my website successfully. 1. First of all you need to create a project/app in firebase console(Firebase Console).

After creating app/project you will see like myfirstfbhosting-b0ae...... Now you are ready to install tools to upload the files of your website

  1. Install the Firebase CLI

The Firebase CLI (Command Line Interface) requires Node.js and npm, which can both be installed by following the instructions on Node.js software. Installing Node.js also installs npm.

The Firebase CLI requires Node.js version 0.10.0 or greater. Once you have Node.js and npm installed, you can install the Firebase CLI via npm:

  1. After installing Firebase CLI with npm

Run the command - "npm install -g firebase-tools" in cmd This installs the globally available firebase command. To update to the latest version, simply re-run the same command.

  1. Initialize your app Once you've chosen the Firebase app you'd like to deploy, cd into your project directory like D:/pradeep/website/ and run the command: "firebase init" in cmd

Running the firebase init command you will see that a "firebase.json" file is created in the root of your project (D:/pradeep/website/) Important Note: Open firebase.json in notepad, If it is {} then edit it to { "hosting": { "public": "." } } and save.

One more thing index.html file along with other files must be in same directory(D:/pradeep/website/)

  1. Select the app which is created in firebase console. If you have created more than 1 app there then select any one app by type in cmd - "firebase list" - all apps list will be shown. then - "firebase use " and then

  2. Deploy your website To deploy your website simply run: write in cmd -"firebase deploy"

And finally Your app will be deployed to the domain .firebaseapp.com



来源:https://stackoverflow.com/questions/38273130/how-to-deploy-an-angular-2-app-using-firebase-hosting

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