[英]Multiple imports of the same file imported as reference, not copy
I have several vuejs reporting components. 我有几个vuejs报告组件。 My intention is to use a common config object to create some consistency across the board and then expand on that on a per-chart basis.
我的意图是使用通用配置对象在整个板上创建某种一致性,然后在每个图表的基础上进行扩展。 The problem I'm running into is that when 1 chart "extends" the configuration, it's impacting others.
我遇到的问题是,当1个图表“扩展”配置时,它会影响其他配置。
For example, one component contains the following JS object which adds a callback for format the label as USD. 例如,一个组件包含以下JS对象,该对象添加了一个回调,用于将标签格式化为USD。
defaultOptions.options.tooltips = {
mode: 'index',
intersect: false,
callbacks: {
label: function(tooltipItem, data) {
// Prefix the tooltip with a dollar sign
return DefaultGraphOptions.formatLabelToDollars(tooltipItem.yLabel);
}
}
};
...but this impacts all charts on the page, not just the chart that contains financials. ...但是这会影响页面上的所有图表,而不仅仅是包含财务的图表。
DefaultGraphOptions DefaultGraphOptions
export default {
options: {
scales: {
xAxes: [{
display: true,
}],
yAxes: [{
display: true,
ticks: {
callback: (label) => {
// format all numbers with commas
let formatter = new Intl.NumberFormat('en-US');
return formatter.format(label);
}
}
}]
},
tooltips: {
callbacks: {
label: (tooltipItem, data) => {
// Format all tooltip figures with commas and such if they're larger numbers
let formatter = new Intl.NumberFormat('en-US');
return formatter.format(tooltipItem.yLabel);
}
}
}
},
formatLabelToDollars: (value) => {
if(parseInt(value) >= 1000){
return '$' + parseInt(value).toLocaleString();
} else {
return '$' + value;
}
},
cancellationReasonColors: () => {
return [
Colors.TEAL,
Colors.BLUE,
Colors.ORANGE,
Colors.PURPLE,
Colors.YELLOW,
Colors.LIME
];
}
}
Here's the component: 这是组件:
import { Bar } from 'vue-chartjs'
import DefaultGraphOptions from './graph-defaults';
import * as Colors from './colors';
export default {
extends: Bar,
data () {
return {
labels: [
{
label: 'Stripe Annual',
borderColor: Colors.PURPLE,
backgroundColor: Colors.PURPLE,
},
{
label: 'Stripe Monthly',
borderColor: Colors.YELLOW,
backgroundColor: Colors.YELLOW,
},
{
label: 'Paypal Annual',
borderColor: Colors.LIME,
backgroundColor: Colors.LIME,
},
],
}
},
mounted () {
// All components clone this object in this way
let defaultOptions = {... DefaultGraphOptions};
defaultOptions.options.title = {
display: true,
text: 'Sales'
};
// Give us summarized tooltips (showing all data sets together)
defaultOptions.options.tooltips = {
mode: 'index',
intersect: false,
callbacks: {
label: function(tooltipItem, data) {
// Prefix the tooltip with a dollar sign
return "$" + tooltipItem.yLabel.toFixed(2);
}
}
};
this.renderChart(DefaultGraphOptions.fromDailyStats(window.salesGrowth, this.labels), defaultOptions.options)
}
}
How can I use the imported DefaultGraphOptions
as a clone on each vuejs compnent so one config doesn't impact another? 如何在每个vuejs组件上将导入的
DefaultGraphOptions
用作克隆,以使一个配置不会影响另一个? It was my understanding that let objClone = { ...obj };
据我了解,
let objClone = { ...obj };
would create a clone of a js object 将创建一个js对象的副本
Export a method instead of an object. 导出方法而不是对象。 This is known as a factory pattern.
这称为工厂模式。
export function createFoo(){
return {foo:new Date()}
}
import createFoo from 'foo'
const foo = createFoo()
This is exactly why Vue's data
must be a function. 这正是Vue的
data
必须是函数的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.