首页 文章

单页聚合物应用程序尝试在部署到Firebase时多次注册元素

提问于
浏览
0

我使用Polymer入门套件作为单页应用程序中路由的参考 . 如果我在Cloud9(我正在使用的开发环境)上运行应用程序,一切都按预期工作 .

我使用聚合物-cli然后 firebase deploy 运行 polymer build . 在Cloud9实例上运行且没有任何问题的相同应用程序会在Firebase托管上产生多个错误 .

Uncaught NotSupportedError: Failed to execute 'registerElement' on 'Document': Registration failed for type 'my-view1'. A type with that name is already registered.

链接按预期导航,但控制台最终加载了上述错误 . 我猜它与firebase.json中的index.html重定向有关 .

firebase.json:

{
  "database": {
    "rules": "database.rules.json"
  },
  "hosting": {
    "public": "build/bundled",
    "rewrites": [
      {
        "source": "**/!{*.*}",
        "destination": "/index.html"
      }
    ]
  }
}

polymer.json:

{
  "entrypoint": "index.html",
  "shell": "src/my-app.html",
  "fragments": [
    "src/my-app.html",
    "src/my-view1.html",
    "src/my-view2.html",
    "src/my-view3.html"
  ],
  "sources": [
    "src/**/*",
    "images/**/*",
    "bower.json"
  ],
  "includeDependencies": [
    "manifest.json",
    "bower_components/webcomponentsjs/webcomponents-lite.min.js"
  ]
}

编辑:这是入门套件的路由部分

<script>
    Polymer({
      is: 'my-app',

      properties: {
        page: {
          type: String,
          reflectToAttribute: true,
          observer: '_pageChanged'
        }
      },

      observers: [
        '_routePageChanged(routeData.page)'
      ],

      _routePageChanged: function(page) {
        this.page = page || 'view1';
      },

      _pageChanged: function(page) {
        // Load page import on demand. Show 404 page if fails
        var resolvedPageUrl = this.resolveUrl('my-' + page + '.html');
        this.importHref(resolvedPageUrl, null, this._showPage404, true);
      },

      _showPage404: function() {
        this.page = 'view404';
      }
    });
  </script>

那里有一个importHref调用,有没有更好的方法来处理这里的路由?

任何提示,技巧或智慧的话都非常感谢 .

1 回答

  • 1

    你用过 importHref 吗?您的 my-view1 已多次注册 . 再次检查以确保 my-view1 未在代码中的某处注册多次 .

相关问题