[英]Global functions in Polymer 2.0
Is there any way to declare a global function in Polymer 2.0 that can be used in other components? 有什么方法可以在Polymer 2.0中声明可以在其他组件中使用的全局函数吗? I have a
moment.html
file for using moment.js in the project: 我在项目中有一个使用moment.js的
moment.html
文件:
<script src="../bower_components/moment/moment.js"></script>
In the same file I also declare a function instead of declaring it in every single component where I want to use it: 在同一文件中,我还声明了一个函数,而不是在要使用它的每个组件中都声明了一个函数:
<script>
function format(date) {
return moment(date).format('dddd, D MMMM YYYY');
}
</script>
The moment
object is available after importing the file but when I try to call the format
function it gives me the warning method 'format' not defined
. 导入文件后,
moment
对象可用,但是当我尝试调用format
函数时,它给了我警告method 'format' not defined
。 How can I make the function publicly available? 如何使该功能公开?
Edit : I can call the format
function from within the script tags of another component, but I cannot access it from within a template, ie with: 编辑 :我可以从另一个组件的脚本标签中调用
format
函数,但是无法从模板中访问它,即:
<strong>[[format(event.date)]]</strong>
I want to render the result from the function on the page, not access it programmatically. 我想从页面上的函数呈现结果,而不是以编程方式访问它。
I think, for your task, the best documentation is Monica Dinculescu's own cheat sheet. 我认为,对于您的任务来说,最好的文档是Monica Dinculescu自己的备忘单。
https://meowni.ca/posts/polymer-2-cheatsheet/#defining-a-mixin https://meowni.ca/posts/polymer-2-cheatsheet/#defining-a-mixin
She's a Polymer developer. 她是聚合物开发人员。 Below is me copy pasting from the link.
下面是我从链接复制粘贴。 It's the
extends MyMixin(Polymer.Element)
that does the magic. 神奇的是
extends MyMixin(Polymer.Element)
。
Defining a class expression mixin to share implementation between different elements: 定义一个类表达式mixin以在不同元素之间共享实现:
<script>
MyMixin = function(superClass) {
return class extends superClass {
// Code that you want common to elements.
// If you're going to override a lifecycle method, remember that a) you
// might need to call super but b) it might not exist
connectedCallback() {
if (super.connectedCallback) {
super.connectedCallback();
}
/* ... */
}
}
}
</script>
Using the mixin in an element definition: 在元素定义中使用mixin:
<dom-module id="element-name">
<template><!-- ... --></template>
<script>
// This could also be a sequence:
//class MyElement extends AnotherMixin(MyMixin(Polymer.Element)) { … }
class MyElement extends MyMixin(Polymer.Element) {
static get is() { return 'element-name' }
/* ... */
}
customElements.define(MyElement.is, MyElement);
</script>
</dom-module>
Here is the sample how I work it ; 这是我工作的例子;
<paper-button on-tap="customfunc"> Test </paper-button>
<div><strong>[[format(date)]]</strong></div> // result at screen: Saturday, 20 January 2018
...
<script>
class MyTest extends Polymer.Element {
static get is() { return 'test-component'; }
ready(){
super.ready();
this.set('date', new Date())
}
customfunc() {
var d = new Date();
var dd = this.format(d);
console.log("d ", d, " - dd = ", dd);// d Sat Jan 20 2018 17:02:38 GMT+0300 (Türkiye Standart Saati) - dd = Saturday, 20 January 2018
}
format(date){
return moment(date).format('dddd, D MMMM YYYY');
}
As Your format_function
is in shadow root, you have to use .shadowRoot.querySelector
由于您的
format_function
位于影子根目录中,因此必须使用.shadowRoot.querySelector
below is my working code, in this i have format_funtion
in page1
and am calling it in page2
下面是我的工作代码,在此我在
page1
有format_funtion
并在page2
调用它
<script src="https://polygit.org/components/webcomponentsjs/webcomponents-loader.js"></script> <link rel="import" href="https://polygit.org/components/polymer/polymer-element.html"> <!-- my-app element --> <dom-module id="my-app"> <template> <my-page1></my-page1> <my-page2></my-page2> </template> <script> class MyApp extends Polymer.Element { static get is() { return 'my-app' } constructor() { super(); } } window.customElements.define(MyApp.is, MyApp); </script> </dom-module> <!-- page1 element --> <dom-module id="my-page1"> <script> class MyPage1 extends Polymer.Element { static get is() { return 'my-page1'; } constructor() { super(); } format_function() { alert("in format function"); } } window.customElements.define(MyPage1.is, MyPage1); </script> </dom-module> <!-- page2 element --> <dom-module id="my-page2"> <template> <div on-click="test">click to test format_funtion.....!</div></template> <script> class MyPage2 extends Polymer.Element { static get is() {return 'my-page2';} test() { var host = document.querySelector('my-app').shadowRoot.querySelector('my-page1'); host. format_function(); } } window.customElements.define(MyPage2.is, MyPage2); </script> </dom-module> <!-- calling element --> <my-app></my-app>
Don't forget to import files eg
page.html
orpage2.html
不要忘记导入文件,例如
page.html
或page2.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.