canjs / can-component

Custom elements and widgets
https://canjs.com/doc/can-component.html
MIT License
8 stars 8 forks source link

Cannot programmatically create Components using leakScope #309

Closed phillipskevin closed 6 years ago

phillipskevin commented 6 years ago

This code throws an error:

import { Component } from "//unpkg.com/can@5/core.mjs";

const C = Component.extend({
  tag: "a-pp",
  view: `hello`,
  ViewModel: {},
  leakScope: true // this causes an error
});

const c = new C();

The error is:

Uncaught TypeError: Cannot read property 'add' of undefined
    at Constructor.setup (VM998 core.mjs:26569)
    at Function.newInstance (VM998 core.mjs:10058)
    at Constructor.init (VM998 core.mjs:10412)
    at new Constructor (eval at <anonymous> (VM998 core.mjs:9851), <anonymous>:3:34)
    at Object.a-pp (VM998 core.mjs:26389)
    at HTMLElement.CustomElement.connectedCallback (VM998 core.mjs:17201)
    at Object.tag (VM998 core.mjs:17205)
    at Function.setup (VM998 core.mjs:26385)
    at Function.extend (VM998 core.mjs:10463)
    at VM997 pen.js:3

here: https://github.com/canjs/can-component/blob/17ac553c02652634eb551b0d2229391b23f46426/can-component.js#L518

Here is a codepen: https://codepen.io/kphillips86/pen/VVeyVo?editors=0011

justinbmeyer commented 6 years ago

Can you if you supply a scope

phillipskevin commented 6 years ago

Yeah, that does work. I initially thought it didn't because my codepen was still throwing, but it was just because I forgot to remove the component from the html, so the auto-mounted component was throwing. Going to close this.