简体   繁体   English

如何将Angular 6应用程序与Drupal 8模块集成

[英]How to integrate Angular 6 app with Drupal 8 module

I need to wrap up my angular 6 app as Drupal 8 module. 我需要将我的angular 6应用程序包装为Drupal 8模块。 I tried to add build files (js, CSS, img) of angular application in Drupal module and defined it as libraries 我试图在Drupal模块中添加角度应用程序的build files (js,CSS,img),并将其定义为库

In drupal_angular.libraries.yml 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

Then, in my PHP code, I created a form and added this 然后,在我的PHP代码中,创建了一个表单并将其添加

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

but, I am getting nothing here. 但是,我什么都没得到。 I want to render my Angular application inside the Drupal(as a module). 我想在Drupal(作为模块)中渲染我的Angular应用程序。

Am I missing something? 我想念什么吗? Is there a better way to implement this? 有没有更好的方法来实现这一目标? Thanks. 谢谢。

At first you should check that you load all the necessary scripts in the right order and that the paths are correct. 首先,您应该检查是否以正确的顺序加载了所有必要的脚本,并且路径正确。 For example, for the Angular 6 quick start project named my-app , and placed in the app directory in Drupal module, it should looks like this: 例如,对于名为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: {}

Also what is you probably missing is a HTML tag to which the Angular will attach the root application component. 另外,您可能缺少的是Angular将根应用程序组件附加到的HTML标签。

I suggest to create custom block in a drupal module, something like that: 我建议在drupal模块中创建自定义块,如下所示:

drupal_angular/src/Plugin/Block/AngularAppBlock.php drupal_angular / SRC /插件/块/ 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
        ],
      ],
    ];
  }

}

Then you can place this block with the Angular app anywhere you like through the Drupal admin interface. 然后,您可以通过Drupal管理界面将其放置在Angular应用程序中的任何位置。


Alternatively you can create a form: 或者,您可以创建一个表单:

drupal_angular/src/Form/AngularAppForm.php drupal_angular / src目录/表格/ 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.
    }

}

And then, for example, just put this form in the routing: 然后,例如,只需将以下表单放入路由中:

drupal_angular/drupal_angular.routing.yml 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'

Then check that you have actually enabled your module at /admin/modules page. 然后在/admin/modules页面上检查您是否已真正启用了模块。

Also you may need to clear the site cache on the /admin/config/development/performance . 另外,您可能需要清除/admin/config/development/performance上的站点缓存。


So it might look something like this: 因此可能看起来像这样:

在此处输入图片说明

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

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