如何在 PHP 应用程序中使用 Angular 2?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38717556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:41:23  来源:igfitidea点击:

How can I use Angular 2 in PHP application?

phpangular

提问by m6devin

I am a PHP developer and I started learning Angular2. But I don't know how to use it with PHP. Is it possible using Angular just as frontend? What I must to do? How to use it on shared hosting without Node.js installed?

我是一名 PHP 开发人员,我开始学习 Angular2。但我不知道如何在 PHP 中使用它。是否可以将 Angular 用作前端?我必须做什么?如何在没有安装 Node.js 的共享主机上使用它?

回答by sitesbyjoe

My 2 cents as a long time PHP developer who has been playing with Angular2 quite a bit.

我的 2 美分作为长期使用 Angular2 的 PHP 开发人员。

As a PHP developer, you're expecting to have your PHP render an HTML page and send to the client. You won't be doing that with Angular2. All that processing that would take place in PHP, building data tables, lists, or whatever is now Angular's job.

作为 PHP 开发人员,您希望 PHP 呈现 HTML 页面并将其发送到客户端。你不会用 Angular2 那样做。所有将在 PHP 中进行的处理、构建数据表、列表或任何现在都是 Angular 的工作。

The only thing you're gonna do with PHP now is simply send JSON responses. Others have said this already above.

您现在唯一要用 PHP 做的事情就是简单地发送 JSON 响应。其他人已经在上面说过了。

I'm assuming that since you're asking this you have little to no experience working with Angular2. So here's the deal:

我假设既然你问这个,你几乎没有使用 Angular2 的经验。所以这是交易:

Learn how to use Node and NPM on your local machine. Learn how to use NPM to set up your empty Angular2 project. Play and develop on your local machine.

了解如何在本地机器上使用 Node 和 NPM。了解如何使用 NPM 设置空的 Angular2 项目。在您的本地机器上玩和开发。

When your ready to load in data, PHP can get involved by sending JSON data down to the front end for Angular to use.

当您准备好加载数据时,PHP 可以通过将 JSON 数据发送到前端供 Angular 使用来参与进来。

When you're ready to put your Angular2 app online for the world you have a number of build options. You need to compile your code from Typescript to Javascript.

当您准备好将您的 Angular2 应用程序上线面向全世界时,您有许多构建选项。您需要将代码从 Typescript 编译为 Javascript。

I've been using the Angular CLI tool. That lets me just run "ng build" and the app gets compiled.

我一直在使用 Angular CLI 工具。这让我只需运行“ng build”即可编译应用程序。

Then I can upload the folder it generates up to my apache server and it works. The Angular CLI makes a folder called "dist" that contains all the stuff your front end will need.

然后我可以将它生成的文件夹上传到我的 apache 服务器并且它可以工作。Angular CLI 创建了一个名为“dist”的文件夹,其中包含您的前端需要的所有内容。

Piece of cake.

小菜一碟。

回答by Borjante

Preferably, your site will just download a simple index.html and a file called app.js which contains all of your JS and therefore your Angular app.

最好,您的站点将只下载一个简单的 index.html 和一个名为 app.js 的文件,其中包含您的所有 JS 以及您的 Angular 应用程序。

PHP will be sitting on a server doing the job of an API, which is answering with JSON/XML to request, you angular app will then use the JSON to build the web interface.

PHP 将位于执行 API 工作的服务器上,该服务器使用 JSON/XML 响应请求,然后您的 Angular 应用程序将使用 JSON 来构建 Web 界面。

You can have PHP hosted anywhere, and serve your angular app from another place, even thought it's not recommended because of latency

您可以将 PHP 托管在任何地方,并从另一个地方提供您的 Angular 应用程序,甚至认为由于延迟而不推荐这样做

<html>
  <script src="app.js">  
</html>

回答by inzanez

as far as I know AngularJS is client side Javascript framework. So in general there's no need for 'nodejs'. You just need the AngularJS library files included in your HTML that's going to be produced by your PHP code.

据我所知 AngularJS 是客户端 Javascript 框架。所以一般来说不需要'nodejs'。您只需要包含在您的 HTML 中的 AngularJS 库文件,这些文件将由您的 PHP 代码生成。

I guess you should have a closer look at AngularJS at first.

我想你应该首先仔细看看 AngularJS。

回答by hoogw

above [sitesbyjoe] best answer is awesome. Just details steps,

以上 [sitesbyjoe] 最佳答案很棒。只是细节步骤,

open cmd

打开cmd

1) cd your-project folder

1) cd your-project 文件夹

2) ng build

2)ng构建

3) copy dist folder to apache/htdocs/[dist/or your-project-name]

3) 将 dist 文件夹复制到 apache/htdocs/[dist/or your-project-name]

4) Important:  open the index.html file, find <base href="/">
change it to <base href="/your-project-name/">
Without doing this, js file will not be load correctly.

5) http://localhost:80/your-project-name/index.html

5) http://localhost:80/your-project-name/index.html

It works.

有用。