简体   繁体   English

如何将 Angular 项目集成到 Squarespace 自定义模板中

[英]How to integrate Angular project into Squarespace custom template

Goal:目标:

I am trying to create a single custom page on my Squarespace site that has a high amount of user interaction supported.我正在尝试在我的 Squarespace 网站上创建一个支持大量用户交互的自定义​​页面。 Because Angular make building single page applications pretty easy, I was hoping to be able to integrate this using Squarespace's developer mode.因为 Angular 使构建单页应用程序变得非常容易,我希望能够使用 Squarespace 的开发人员模式来集成它。

Technical Feasibility:技术可行性:

It is my understanding that this should be possible because angular compiles down to javascript and Squarespace support custom javascript.我的理解是这应该是可能的,因为 angular 编译为 javascript,而 Squarespace 支持自定义 javascript。 I have also seen this SO question where another community member has also communicated that it should be possible: Integrating Angular CLI with Squarespace我也看到了这个 SO 问题,其中另一位社区成员也表示这应该是可能的: Integrating Angular CLI with Squarespace

Squarespace Setup:方形空间设置:

I do have a premium account and have enabled developer mode .我确实有一个高级帐户并启用了开发人员模式 I have also cloned down the Squarespace project and was able to successfully run the project locally on my machine.我还克隆了 Squarespace 项目,并能够在我的机器上本地成功运行该项目 Lastly, I have been able to follow the Squarespace's instructions to create a static page where I can put custom html and javascript.最后,我已经能够按照 Squarespace 的说明创建一个静态页面,我可以在其中放置自定义 html 和 javascript。 No problems up to this point.到目前为止没有问题。

Angular Setup:角度设置:

To keep things simple, I created a new angular app and immediately ran ng build to compile it to javascript .为简单起见,我创建了一个新的 angular 应用程序并立即运行 ng build 将其编译为 javascript This creates all the necessary files in the /dist/ directory.这将在 /dist/ 目录中创建所有必需的文件。

The issue I am having is getting an angular app to load on one of these custom pages.我遇到的问题是在这些自定义页面之一上加载一个有角度的应用程序。

Attempts at Angular / Squarespace Integration: Angular / Squarespace 集成的尝试:

Trial 1:试验 1:

The first thing I had tried was copying the contents of the /dist/ directory into Squarespace's pages directory.我尝试的第一件事是将 /dist/ 目录的内容复制到 Squarespace 的 pages 目录中。 Because the custom page was already created and working in the app, I then took the content's of index.html's body and placed them inside the static page.因为自定义页面已经创建并在应用程序中工作,所以我将 index.html 正文的内容放入静态页面中。

  <app-root></app-root>
  <script type="text/javascript" src="inline.bundle.js"></script>
  <script type="text/javascript" src="polyfills.bundle.js"></script>
  <script type="text/javascript" src="styles.bundle.js"></script>
  <script type="text/javascript" src="vendor.bundle.js"></script>
  <script type="text/javascript" src="main.bundle.js"></script>

This did not work because squarespace uses it's own url path generation so my scripts were being loaded relative to my custom page's path and not the root of the server.这不起作用,因为 squarespace 使用它自己的 url 路径生成,所以我的脚本是相对于我的自定义页面的路径而不是服务器的根目录加载的。

Trial 2:试验 2:

One of the things I had noticed, was that Squarespace provides a /scripts/ directory to store custom scripts.我注意到的一件事是 Squarespace 提供了一个 /scripts/ 目录来存储自定义脚本。 After reading the Squarespace documentation about custom scripts , I moved my js files into the scripts directory and updated my script tags to follow their guidelines:在阅读了关于自定义脚本Squarespace 文档后,我将我的 js 文件移动到脚本目录并更新了我的脚本标签以遵循他们的指导方针:

<app-root></app-root>
<squarespace:script src="inline.bundle.js" combo='true'></script>
<squarespace:script src="polyfills.bundle.js" combo='true'></script>
<squarespace:script src="styles.bundle.js" combo='true'></script>
<squarespace:script src="vendor.bundle.js" combo='true'></script>
<squarespace:script src="main.bundle.js" combo='true'></script>

This did fix the 404 issue where the scripts could not be found, but now chrome is reporting:这确实解决了找不到脚本的 404 问题,但现在 chrome 正在报告:

bootstrap ff237d7…:19 Uncaught TypeError: parentJsonpFunction is not a function
    at webpackJsonpCallback (bootstrap ff237d7…:19)
    at polyfills.bundle.js:1

Trial 3:试验 3:

Thinking that maybe the issue is related to an invalid path, I tried recompiling the angular app using the base-href flag .考虑到问题可能与无效路径有关,我尝试使用base-href 标志重新编译 angular 应用程序。 Because Squarespace requires the files be placed in the /scripts/ directory, that was the value I set as the base-href:因为 Squarespace 要求将文件放置在 /scripts/ 目录中,所以我将其设置为 base-href 的值:

ng build --base-href=/scripts/

Unfortunately, this had no effect.不幸的是,这没有效果。 Chrome continues to report the same error. Chrome 继续报告相同的错误。

This code is generated during the angular app compilation so it does not seem to a good idea to debug or modify these files.此代码是在 angular 应用程序编译期间生成的,因此调试或修改这些文件似乎不是一个好主意。

Next Steps?下一步?

My question is, how do overcome this error and what should I do differently that would allow these files to load without issue?我的问题是,如何克服这个错误,我应该怎么做才能让这些文件毫无问题地加载?

Thanks for the help!谢谢您的帮助!

What appears to be happening is that the value window["webpackJsonp"] is already set to an invalid value from loading the template shell.似乎正在发生的事情是值 window["webpackJsonp"] 已经从加载模板 shell 设置为无效值。 By unsetting this value, it does let the compiled angular files load successfully.通过取消设置这个值,它确实让编译的角度文件成功加载。 My custom page now looks like我的自定义页面现在看起来像

<app-root></app-root>
<script>window["webpackJsonp"] = undefined;</script>
<squarespace:script src="inline.bundle.js" combo="false"/>
<squarespace:script src="polyfills.bundle.js" combo="false"/>
<squarespace:script src="styles.bundle.js" combo="false"/>
<squarespace:script src="vendor.bundle.js" combo="false"/>
<squarespace:script src="main.bundle.js" combo="false"/>

However, I am not certain this is the best solution.但是,我不确定这是最好的解决方案。 It is unclear what the original value window["webpackJsonp"] was used for or if it is still needed for some other functionality within the Squarespace template.目前尚不清楚原始值 window["webpackJsonp"] 用于什么,或者是否仍然需要 Squarespace 模板中的某些其他功能。 For that reason, I would still encourage other community members to recommend better solutions or suggestions to improve this one.出于这个原因,我仍然会鼓励其他社区成员推荐更好的解决方案或建议来改进这个。

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

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