【问题标题】:How to integrate Angular 6 app with Drupal 8 module如何将 Angular 6 应用程序与 Drupal 8 模块集成
【发布时间】:2018-08-28 23:01:58
【问题描述】:

我需要将我的 Angular 6 应用程序打包为 Drupal 8 模块。我尝试在 Drupal 模块中添加 Angular 应用程序的 build files (js, CSS, img) 并将其定义为库

drupal_angular.libraries.yml

drupal_angular_lib:
  version: 1.x
  css:
    theme:
      css/bootstrap.min.css: {}
      css/demo.css: {}
      css/paper-dashboard.css: {}
      css/styles.a3a18ed7bbe6e603703b.css: {}
      css/themify-icons.css: {}
  js:
    js/main.5c5c8e529add0697c1bf.js: {}
    js/polyfills.a75b8d97ee951ee6b5c4.js: {}
    js/runtime.a66f828dca56eeb90e02.js: {}
    js/scripts.542a57744863c85b72b6.js: {}
  dependencies:
    - core/jquery
    - core/drupalSettings

然后,在我的 PHP 代码中,我创建了一个表单并添加了这个

$form['#attached']['library'][] = 'drupal_angular/drupal_angular_lib';

但是,我在这里什么也得不到。我想在 Drupal 中渲染我的 Angular 应用程序(作为一个模块)。

我错过了什么吗?有没有更好的方法来实现这一点?谢谢。

【问题讨论】:

  • 将 console.log 消息添加到 Angular 的主模块并确保文件正在加载,然后确保 Angular 用于引导的 HTML 标记存在。
  • make sure that the HTML tag that Angular is using to bootstrap exists. 我如何检查这个?我会在console.log() 得到那个吗?
  • 在主 NgModule 中查看用于引导模块的组件。它位于bootstrap: 部分。必须有一个与该组件使用的选择器匹配的 HTML 标记。

标签: angular drupal angular6 drupal-modules drupal-8


【解决方案1】:

首先,您应该检查是否以正确的顺序加载了所有必要的脚本,并且路径是否正确。例如,对于名为my-app 的Angular 6 快速入门项目,它位于Drupal 模块的app 目录中,它应该如下所示:

drupal_angular_lib:
  version: 1.x
  js:
    app/dist/my-app/runtime.js: {}
    app/dist/my-app/polyfills.js: {}
    app/dist/my-app/styles.js: {}
    app/dist/my-app/vendor.js: {}
    app/dist/my-app/main.js: {}

您可能还缺少一个 HTML 标记,Angular 会将根应用程序组件附加到该标记上。

我建议在 drupal 模块中创建自定义块,类似这样:

drupal_angular/src/Plugin/Block/AngularAppBlock.php

<?php

namespace Drupal\drupal_angular\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
 * @Block(
 *   id = "drupal_angular",
 *   admin_label = @Translation("Drupal Angular"),
 *   category = @Translation("Custom"),
 * )
 */
class AngularAppBlock extends BlockBase
{

  /**
   * {@inheritdoc}
   */
  public function build()
  {
    return [
      '#type' => 'html_tag',
      '#tag' => 'app-root', // Selector of the your app root component from the Angular app
      '#attached' => [
        'library' => [
          'drupal_angular/drupal_angular_lib', // To load the library only with this block
        ],
      ],
    ];
  }

}

然后,您可以通过 Drupal 管理界面将此块与 Angular 应用程序放在您喜欢的任何位置。


您也可以创建一个表单:

drupal_angular/src/Form/AngularAppForm.php

<?php

namespace Drupal\drupal_angular\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

class AngularAppForm extends FormBase
{

    /**
     * {@inheritdoc}
     */
    public function getFormId()
    {
        return 'drupal_angular_app_form';
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(array $form, FormStateInterface $form_state)
    {
        $form[] = [
          '#type' => 'html_tag',
          '#tag' => 'app-root',
          '#attached' => [
            'library' => [
              'drupal_angular/drupal_angular',
            ],
          ],
        ];
        return $form;
    }

    /**
     * {@inheritdoc}
     */
    public function validateForm(array &$form, FormStateInterface $form_state)
    {
        // TODO.
    }

    /**
     * {@inheritdoc}
     */
    public function submitForm(array &$form, FormStateInterface $form_state)
    {
        // TODO.
    }

}

然后,例如,只需将此表单放入路由中:

drupal_angular/drupal_angular.routing.yml

drupal_angular.app_form:
  path: '/drupal-angular' # Path that you want for your form
  defaults:
    _title: 'Drupal Angular Form'
    _form: '\Drupal\drupal_angular\Form\AngularAppForm'
  requirements:
    _permission: 'access content'

然后检查您是否在/admin/modules 页面上实际启用了您的模块。

您还可能需要清除/admin/config/development/performance 上的站点缓存。


所以它可能看起来像这样:

【讨论】:

  • 非常感谢,它成功了。我的 Angular 应用程序中确实有 assets 文件夹,其中包含图像、sass 等。你能帮我添加这些文件吗?非常感谢。
  • @NavaneethSelvaraj Drupal 8 提供来自sites/default/files 目录的公共静态文件。我认为将 Angular 资产输出放在那里是最简单的。您可以手动复制它,将其设置为额外的构建步骤,或者创建从文件目录到 Angular 输出的符号链接,或者在服务器上使用路径重写......有很多方法可以做到这一点。然后,您需要在 Angular 应用程序中的资产 URL 中添加适当的前缀......我不确定在 Angular 6 中如何准确地配置它。也许您可以只使用资产的绝对路径,从 /sites/default/files/ 开始.
  • 感谢您的意见。我有一个用例,我需要从 Angular 向 Drupal 模块发送和接收数据,我相信如果我将此模块实现为一个块,我就不能这样做。您能否帮助我将模块创建为表单或以其他方式,以便我能够使用 PHP 中的代理发送和接收数据。谢谢。
  • @NavaneethSelvaraj,我添加了表单示例。但是对于与您的 Drupal 站点的任何复杂交互,您可能应该在您的 Angular 应用程序中创建一个服务,该服务将使用 Drupal REST API。见drupal.org/docs/8/core/modules/rest
  • 或者,您可以在模块中创建自定义 API 控制器以满足 Angular 应用程序的需要,而不是使用 Drupal REST API。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-30
  • 1970-01-01
  • 2011-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多