简体   繁体   English

Polymer 2.0中的factoryImplementation

[英]factoryImplementation in Polymer 2.0

In Polymer 1.0 there was the possibility to detect if a polymer element was created by contructor, the function that was triggered when the element was created named factoryImpl(). 在Polymer 1.0中,可以检测是否由构造器创建了聚合物元素,当创建该元素时触发的函数名为factoryImpl()。

I wan't to do the same in Polymer 2.0, if i create an element via a constructor, a standard function should be triggered and should do something. 我不会在Polymer 2.0中做同样的事情,如果我通过构造函数创建了一个元素,则应该触发一个标准函数并且应该做一些事情。 Does anyone done this before and can give a hint to do that? 有没有人做过此事并且可以给出提示呢?

Thanks a lot 非常感谢

You can use constructor: 您可以使用构造函数:

class TestEle extends Polymer.Element {
      static get is() { return 'test-ele'; }
      constructor() {
        super()
        console.log('created')
      }
//...

You should see 'created' logged whenever you create TestEle 每当您创建TestEle时,您都应该看到“创建”已记录

<test-ele> </test-ele> // created
or 
document.createElement('test-ele') // created
or
new TestEle() // created

// Edited as per the comments below. //根据以下评论进行编辑。

I couldn't find any information on the legacy factoryImpl equivalent in 2.0. 我找不到与2.0中旧版factoryImpl等效的任何信息。 However, there's a work around you could try. 但是,您可以尝试以下工作。

class TestEle extends Polymer.Element {
      static get is() { return 'test-ele'; }
      constructor(c) {
        super()
        console.log('created')
        if(c) {
           console.log('created using constructor')
        }
      }
...
<test-ele> </test-ele> // created
or 
document.createElement('test-ele') // created
or
new TestEle(true) // created and created using constructor

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

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