简体   繁体   English

如何使用 Laravel 作为 Angular SPA 应用程序的后端?

[英]How to use Laravel as the backend for an Angular SPA app?

I would like to create a website with Angular (frontend) and Laravel (backend).我想用 Angular(前端)和 Laravel(后端)创建一个网站。 What is the best way to connect these two?连接这两者的最佳方式是什么? How do they communicate?他们如何沟通? What I found while searching was more than 2 years old solutions, so I am wondering if there is a preferred way to do that now.我在搜索时发现的是 2 年多以前的解决方案,所以我想知道现在是否有首选的方法来做到这一点。

What I do right now: I created Model, Controller and Migration files in laravel, and in LanguagesController I call the "::all" method to get all the data from XAMPP:我现在做什么:我在 laravel 中创建了模型、控制器和迁移文件,并在LanguagesController 中调用“::all”方法从 XAMPP 获取所有数据:

public function index()
{

    return response()->json(Languages::all(), 200, ['Content-Type' => 'application/json;charset=UTF-8', 'Charset' => 'utf-8'],
    JSON_UNESCAPED_UNICODE);

}

After that I create route for it in api.php :之后我在api.php 中为它创建路由:

Route::get('/languages', "LanguagesController@index");

So now, I can call www.laravelfirsttry.com/api/languages to get all the data from Languages table.所以现在,我可以调用 www.laravelfirsttry.com/api/languages 从 Languages 表中获取所有数据。

In Angular, I did the following: Create a language-service which send a request to Laravel:在 Angular 中,我做了以下事情:创建一个向 Laravel 发送请求的语言服务

private baseUrl = 'http://www.laravelfirsttry.com/api/languages';

constructor(private http: HttpClient) { }

getLanguages() {
  return this.http.get(this.baseUrl);
}

Then in my component I call this service, and give its function's response to my variable then I just handle the view after that:然后在我的组件中调用此服务,并将其函数的响应提供给我的变量,然后我只处理此后的视图:

constructor(private languageService: LanguageService) { 
this.getLanguages();
}
getLanguages(): void{
this.languageService.getLanguages()
  .subscribe((res: any) => {
    this.language_tabs = res;
    console.log(res);
  }, error => {
    console.error(error);
  });
}

But the problem is that when I try it, the api call takes relatively long time to finish .但问题是,当我尝试时,api 调用需要相对较长的时间才能完成 I load my website, all the content is loaded, but the main component is not visible until the call is finished.我加载了我的网站,加载了所有内容,但在调用完成之前主要组件不可见。 I think this does not look good, and I think I did something wrong.我觉得这看起来不太好,我觉得我做错了什么。

Summary : I created one API in laravel , then I call this API in Angular , and its quite slow.总结:我在laravel 中创建了一个API ,然后我在Angular 中调用了这个 API,而且速度很慢。 What is the best way to make an Angular application with Laravel backend?使用 Laravel 后端制作 Angular 应用程序的最佳方法是什么?

First, you need HttpClientModule to the imports array of your AppModule, something like this.首先,您需要 HttpClientModule 到您的 AppModule 的导入数组,就像这样。

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';

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

@NgModule({
    imports: [
        BrowserModule,
        HttpClientModule
    ],
    declarations: [
        AppComponent
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

Then import httpClient in your component and add it to the constructor然后在您的组件中导入 httpClient 并将其添加到构造函数中

import { HttpClient } from '@angular/common/http';

Then you can call API with code like this.然后你可以用这样的代码调用 API。

this.http.get<any>('http://www.laravelfirsttry.com/api/languages').subscribe(data => {
            console.log(data);
            // do any operations with your data.
        })

As a pre-requisite Please make sure that your backend Laravel API is working fine.作为先决条件,请确保您的后端 Laravel API 工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM