简体   繁体   English

集成环回 4 和 Angular 8

[英]Integrating Loopback 4 and Angular 8

I am new to loopback.我是环回的新手。 Started with Loopback 4. I am trying to create a simple 'Hello world' application using Loopback4 and Angular8.从 Loopback 4 开始。我正在尝试使用 Loopback4 和 Angular8 创建一个简单的“Hello world”应用程序。

In my application.ts, I am pointing my static directive path to my angular application src folder.在我的 application.ts 中,我将 static 指令路径指向我的 angular 应用程序 src 文件夹。

// Set up default home page
    this.static('/', path.join(__dirname, '../angular-app/src'));

So I can see normal HTML content from index.html file from angular-app/src/index.html file, but it doesn't bind anything inside <app-root> </app-root>所以我可以从 angular-app/src/index.html 文件中的 index.html 文件中看到正常的 HTML 内容,但它没有绑定<app-root> </app-root>中的任何内容

I see in Loopback v3, middleware.json was used to host client application like this:我在 Loopback v3 中看到,middleware.json 用于托管客户端应用程序,如下所示:

"files": {
"loopback#static": {
"params": "$!../client"
 }
}

What I'm doing wrong here?我在这里做错了什么?

Thanks in advance.提前致谢。

Do the following:请执行下列操作:

  • In terminal move to root app directory在终端移动到根应用程序目录

  • Run

    $ng build

  • Replace代替

    this.static('/', path.join(__dirname, '../angular-app/src'));

    with

    this.static('/', path.join(__dirname, '../angular-app/www/index.html'));

Then test.然后测试。

My guess is you are not building the angular project before putting your loopback server online.我的猜测是在将环回服务器联机之前,您没有构建 angular 项目。 Whatever endpoint on which you are serving the angular application is sending a bunch of.ts files which browsers are not capable of displaying.无论您在哪个端点上为 angular 应用程序提供服务,都会发送一堆浏览器无法显示的 .ts 文件。

If the path I wrote is not the one, you might want to look for a new folder inside your project that wasn't there before you ran the build command.如果我写的路径不是那个,你可能想在你的项目中寻找一个在你运行构建命令之前不存在的新文件夹。 Inside there should be some directories, .js files with weird names and an index.html file.里面应该有一些目录、名称奇怪的 .js 文件和一个 index.html 文件。

I've managed to get it work in Loopback 4 with Angular 9. After an angular app ng build the contents of the dist/\<app-name\> folder should be placed inside the loopback public folder.我已经设法让它在带有 Angular 9 的 Loopback 4 中工作。在 angular app ng builddist/\<app-name\>文件夹的内容应该放在 loopback public文件夹中。

Then, in order for the angular router to work properly, all the HTML errors ( Specifically 404 Not found errors) should be redirected to the angular index.html . Then, in order for the angular router to work properly, all the HTML errors ( Specifically 404 Not found errors) should be redirected to the angular index.html .

To do that we must bind RestBindings.SequenceActions.REJECT to a custom provider.为此,我们必须将RestBindings.SequenceActions.REJECT绑定到自定义提供程序。 In application.ts just include the new bindingapplication.ts中只包含新的绑定

this.bind(RestBindings.SequenceActions.REJECT).toInjectable(AngularRejectProvider);

The AngularRejectProvider could be taken from the default reject provider in @loopback/rest/src/providers/reject.provider.ts : AngularRejectProvider可以取自@loopback/rest/src/providers/reject.provider.ts中的默认拒绝提供程序:

// Copyright IBM Corp. 2018,2020. All Rights Reserved.
// Node module: @loopback/rest
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {BindingScope, inject, injectable} from '@loopback/core';
import {HttpError} from 'http-errors';
import {ErrorWriterOptions, writeErrorToResponse} from 'strong-error-handler';
import {RestBindings} from '@loopback/rest';
import {HandlerContext, LogError, Reject} from '@loopback/rest';
// @ts-ignore
var accepts = require('accepts')
import path from 'path';

// TODO(bajtos) Make this mapping configurable at RestServer level,
// allow apps and extensions to contribute additional mappings.
const codeToStatusCodeMap: {[key: string]: number} = {
  ENTITY_NOT_FOUND: 404,
};

@injectable({scope: BindingScope.SINGLETON})
export class AngularRejectProvider {
  static value(
    @inject(RestBindings.SequenceActions.LOG_ERROR)
    logError: LogError,
    @inject(RestBindings.ERROR_WRITER_OPTIONS, {optional: true})
    errorWriterOptions?: ErrorWriterOptions,
  ): Reject {
    const reject: Reject = ({request, response}: HandlerContext, error) => {

      // ADD THIS
      const resolvedContentType = accepts(request).types([
        'text/html', 'html',
      ]);
      switch(resolvedContentType) {
        case 'text/html':
        case 'html':
          return response.sendFile(path.join(__dirname, '../../public/index.html'));;
      }
      // END ADD THIS

      const err = <HttpError>error;

      if (!err.status && !err.statusCode && err.code) {
        const customStatus = codeToStatusCodeMap[err.code];
        if (customStatus) {
          err.statusCode = customStatus;
        }
      }

      const statusCode = err.statusCode || err.status || 500;
      writeErrorToResponse(err, request, response, errorWriterOptions);
      logError(error, statusCode, request);
    };
    return reject;
  }
}

The new part is the following which basically redirects all HTML errors to angular:新部分如下,它基本上将所有 HTML 错误重定向到 angular:

const resolvedContentType = accepts(request).types([
            'text/html', 'html',
          ]);
          switch(resolvedContentType) {
            case 'text/html':
            case 'html':
              return response.sendFile(path.join(__dirname, '../../public/index.html'));;
          }

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

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