无法映射数组 javascript
Trouble mapping array javascript
所以我正在尝试将图表构建为 Web 组件,但在映射数组时遇到了一些问题。错误说:this.values.map 不是函数
代码如下:
import {LitElement, html, css} from 'lit-element';
class ApexChart extends LitElement {
static get properties(){
return{
values: {Array},
labels: {Array}
};
}
constructor(){
super();
this.values = [];
this.labels =[];
}
static get styles(){
return css `
`;
}
render(){
return html`
<p>${this.values.map(value => {value + 1})}</p>
`;
}
}
customElements.define('apex-chart', ApexChart);
我正在传递来自 html
的值
<apex-chart values="[1,2,3,4]" labels="['Hi', 'Hello', 'Oi', 'Hola']"></apex-chart>
我看不出我做错了什么
您有两个问题:
1) properties
type converter 定义不正确。应该是:
static get properties(){
return{
values: { type: Array },
labels: { type: Array }
};
}
2) 地图方法工作不正常。目前它 returns undefined
值,因为如果你使用 {}
你必须使用 return
关键字。
> [0, 1].map(value => { value + 1 })
<- (2) [undefined, undefined]
改为使用:
render(){
return html`
<p>${this.values.map(value => value + 1)}</p>
`;
}
或:
render(){
return html`
<p>${this.values.map(value => { return value + 1; })}</p>
`;
}
所以我正在尝试将图表构建为 Web 组件,但在映射数组时遇到了一些问题。错误说:this.values.map 不是函数
代码如下:
import {LitElement, html, css} from 'lit-element';
class ApexChart extends LitElement {
static get properties(){
return{
values: {Array},
labels: {Array}
};
}
constructor(){
super();
this.values = [];
this.labels =[];
}
static get styles(){
return css `
`;
}
render(){
return html`
<p>${this.values.map(value => {value + 1})}</p>
`;
}
}
customElements.define('apex-chart', ApexChart);
我正在传递来自 html
的值<apex-chart values="[1,2,3,4]" labels="['Hi', 'Hello', 'Oi', 'Hola']"></apex-chart>
我看不出我做错了什么
您有两个问题:
1) properties
type converter 定义不正确。应该是:
static get properties(){
return{
values: { type: Array },
labels: { type: Array }
};
}
2) 地图方法工作不正常。目前它 returns undefined
值,因为如果你使用 {}
你必须使用 return
关键字。
> [0, 1].map(value => { value + 1 })
<- (2) [undefined, undefined]
改为使用:
render(){
return html`
<p>${this.values.map(value => value + 1)}</p>
`;
}
或:
render(){
return html`
<p>${this.values.map(value => { return value + 1; })}</p>
`;
}