gpt4 book ai didi

javascript - 如何覆盖 Polymer 2 中的组件功能?

转载 作者:行者123 更新时间:2023-11-30 21:18:50 24 4
gpt4 key购买 nike

我正在使用 app-localstorage-document在浏览器中存储数据。文档说要覆盖 zeroValue 方法来定义在没有存储数据时使用的默认值。但是我不知道如何在 Polymer 中重写组件的方法。这是我尝试过的方法,但我认为它不正确,因为未调用该函数。

<app-localstorage-document key="CatValue" data="{{cat}}"></app-localstorage-document>

class MyApp extends Polymer.Element{
static get is(){return 'my-app';}
static get properties(){
return{
cat:{
type: String,
value: ""
}
};
}

zeroValue(){
this.set('cat',"a cat");
}
}

最佳答案

这可能对您有帮助:

<app-localstorage-document key="CatValue" data="{{cat}}"></app-localstorage-document>

class MyApp extends Polymer.Element{
static get is(){return 'my-app';}
static get properties(){
return{
cat:{
type: String,
value: function() {
return this.zeroValue;
}
}
};
}

//Override the default method
get zeroValue(){
return 'a cat';
}
}

如果您不想覆盖 zeroValue(),它会将键 CatValue 的值存储为未定义。

关于javascript - 如何覆盖 Polymer 2 中的组件功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45392595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com