聚合物不是一个函数:部署流星/火焰/聚合物2应用程序的麻烦



我花了 5-6 天的时间将一个相对较大的传统 Polymer 0.5 应用程序迁移到 Polymer 2,这不是小菜一碟。

在当地一切都很成功。使用新版本在线部署时,我得到了一个新的惊喜,应用程序的 UI 坏了,控制台充满了这种类型的消息:

Uncaught TypeError: Polymer is not a function
at iron-meta.html:125
at iron-meta.html:215
iron-icon.html:146 Uncaught TypeError: Cannot read property 'create' of undefined
at iron-icon.html:146
iron-iconset-svg.html:50 Uncaught TypeError: Polymer is not a function
at iron-iconset-svg.html:50
iron-pages.html:56 Uncaught TypeError: Polymer is not a function
at iron-pages.html:56
iron-image.html:145 Uncaught TypeError: Polymer is not a function
at iron-image.html:145
paper-card.html:167 Uncaught TypeError: Polymer is not a function
at paper-card.html:167
paper-toolbar.html:292 Uncaught TypeError: Polymer is not a function
at paper-toolbar.html:292
paper-ripple.html:440 Uncaught TypeError: Polymer is not a function
at paper-ripple.html:440
at paper-ripple.html:764
paper-button.html:170 Uncaught TypeError: Polymer is not a function
at paper-button.html:170
paper-checkbox.html:239 Uncaught TypeError: Polymer is not a function
at paper-checkbox.html:239
iron-overlay-backdrop.html:62 Uncaught TypeError: Polymer is not a function
at iron-overlay-backdrop.html:62
at iron-overlay-backdrop.html:166
iron-overlay-manager.html:48 Uncaught TypeError: Cannot read property 'add' of undefined
at new Polymer.IronOverlayManagerClass (iron-overlay-manager.html:48)
at iron-overlay-manager.html:382

这样的例子不胜枚举。这种类型的代码上会发生"聚合物不是函数"类型的错误:

<script>
(function() {
'use strict';
Polymer({ <=======
is: 'paper-toast',

我首先无法理解的是开发和生产环境之间的区别来自哪里。 在"网络"选项卡中,我可以看到所有供应商 Web 组件都以相同的顺序加载,并且没有请求失败。它只是在 prod 中不起作用,这使得调试变得复杂。

这是一个Meteor应用程序,有Blaze和Polymer(我知道这是一种糟糕的鸡尾酒)。

代码大致如下所示:

imports.html(简体)

<head>
<script>
window.Polymer = {
lazyRegister: true
};
</script>
<!-- Webcomponents Polyfill -->
<script src="/bower_components/webcomponentsjs/webcomponents-lite.js"></script>
<!-- Polymer elements -->
<link rel="import" href="/bower_components/polymer/polymer.html">
<link rel="import" href="/bower_components/app-layout/app-layout.html">
<link rel="import" href="/bower_components/iron-collapse/iron-collapse.html">
<!-- etc -->
</head>

app.html(应用程序根视图,简化)

<template name="applicationLayout">
<app-drawer-layout>
<!-- Left title and menu -->
<app-drawer slot="drawer">
<app-toolbar></app-toolbar>
{{>menu}}
</app-drawer>
<!-- Main content, page, router yielder -->
<app-header-layout>
<app-header slot="header">
<app-toolbar>
<div>{{>topBarMenus}}</div>
</app-toolbar>
</app-header>
<div>{{>yield}}</div>
</app-header-layout>
</app-drawer-layout>
</template>

<template>和胡子标签是烈焰的东西。

你可以注意到我没有<body><html>或其他包含(对于 Meteor 等),这实际上是我的应用程序的唯一引导代码。我无法控制这一点。Meteor 检测到<head>并注入它,但会自己创建其余部分,插入其样式表等。我不能自己创造它。我知道这很愚蠢。我也不喜欢流星或烈焰。

我知道 Polymer 建议将应用程序根放在<dom-module>中,在另一个 HTML 文件中,加载 HTML 导入等,但我什至不能这样做(感谢 Meteor),我也不能重写整个应用程序(尽管我想)。

有解决方法吗?我的意思是,它正在开发中。什么原因可能导致这种情况?它发生在所有浏览器中。

好吧,那天我似乎很累,两天后回到这个问题后,我在大约 20 分钟内找到并解决了这个问题。

就我而言,我的bower.json中缺少一个Webcomponent bower包。它已安装在我的机器上,但忘记将其(bower i -S)保存到清单中。因此,在生产中,它没有安装。

在我的特定情况下,当找不到请求的文件时,我的 Meteor 设置会发送 200 个带有索引文件的状态代码。这消除了我通常因缺少包裹而遇到的错误。但它使 Polymer 初始化以非显式方式崩溃。

寓意是:

仔细检查您的包裹!!版本、对等依赖关系、缺失的依赖关系等。

如果可能的话,使用另一个包管理器而不是Bower(太糟糕的聚合物仍然依赖于它)或npm,这是非常糟糕的,只使用纱线。

最新更新