Highcharts Vuejs组件通信
我正在尝试使用$emit和$on在两个组件之间进行通信: 我无法在两个组件之间进行通信,也无法从组件A中的单击事件更新组件B中的高图表 组件A的JavaScript代码:Highcharts Vuejs组件通信,highcharts,vuejs2,Highcharts,Vuejs2,我正在尝试使用$emit和$on在两个组件之间进行通信: 我无法在两个组件之间进行通信,也无法从组件A中的单击事件更新组件B中的高图表 组件A的JavaScript代码: import Vue from 'vue'; const bus = new Vue(); const pause = ms => new Promise(resolve => setTimeout(resolve, ms)); export default { data: () => ({
import Vue from 'vue';
const bus = new Vue();
const pause = ms => new Promise(resolve => setTimeout(resolve, ms));
export default {
data: () => ({
active: [],
avatar: null,
open: [],
users: [],
}),
computed: {
items() {
return [
{
name: 'Users',
children: this.users,
},
];
},
selected() {
if (!this.active.length) return undefined;
const id = this.active[0];
return this.users.find(user => user.id === id);
},
},
methods: {
fetchData() {
const id = this.active[0];
this.parts = this.users.find(user => user.id === id);
bus.$emit('new_parts', this.parts.data);
console.log(this.parts.data);
},
async fetchUsers(item) {
// Remove in 6 months and say
// you've made optimizations! :)
await pause(1500);
return fetch('http://localhost:8081/api/toppartsdata')
.then(res => res.json())
.then(json => (item.children.push(...json)))
.catch(err => console.warn(err));
},
},
};
import VueHighcharts from 'vue2-highcharts';
import Vue from 'vue';
const bus = new Vue();
const asyncData = {
name: 'Prediction Chart',
marker: {
symbol: 'circle',
},
data: [],
};
export default {
components: {
VueHighcharts,
},
data() {
return {
options: {
chart: {
type: 'spline',
title: '',
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},
yAxis: {
title: {
text: 'LINECOST',
},
labels: {
formatter() {
return `${this.value}°`;
},
},
},
tooltip: {
crosshairs: true,
shared: true,
},
credits: {
enabled: false,
},
plotOptions: {
spline: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1,
},
},
},
series: [],
},
};
},
methods: {
test() {
// eslint-disable-next-line func-names
bus.$on('new_parts', (data) => {
alert(value);
});
},
load() {
// eslint-disable-next-line func-names
bus.$on('new_parts', function (data) {
this.asyncData.data = data;
});
const { lineCharts } = this.$refs;
lineCharts.delegateMethod('showLoading', 'Loading...');
setTimeout(() => {
lineCharts.addSeries(asyncData.data);
lineCharts.hideLoading();
}, 2000);
},
},
};
<template>
<div>
<vue-highcharts :options="options" ref="lineCharts"></vue-highcharts>
</div>
</template>
<script>
import VueHighcharts from 'vue2-highcharts';
import { bus } from '../../../main';
export default {
props: {
partsdata: {
type: Array,
},
},
components: {
VueHighcharts,
},
data() {
return {
options: {
chart: {
type: 'spline',
title: 'Hassaan',
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},
yAxis: {
title: {
text: '',
},
labels: {
formatter() {
return `${this.value}°`;
},
},
},
tooltip: {
crosshairs: true,
shared: true,
},
credits: {
enabled: false,
},
plotOptions: {
spline: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1,
},
},
},
series: [],
},
};
},
created() {
bus.$on('new_user', (data) => { this.series = data; });
},
};
</script>
created() {
bus.$on('new_user', (data) => { this.series = data; });
}
created() {
bus.$on('new_user', (data) => { this.options.series[0].data = data; });
}
组件B的JavaScript代码:
import Vue from 'vue';
const bus = new Vue();
const pause = ms => new Promise(resolve => setTimeout(resolve, ms));
export default {
data: () => ({
active: [],
avatar: null,
open: [],
users: [],
}),
computed: {
items() {
return [
{
name: 'Users',
children: this.users,
},
];
},
selected() {
if (!this.active.length) return undefined;
const id = this.active[0];
return this.users.find(user => user.id === id);
},
},
methods: {
fetchData() {
const id = this.active[0];
this.parts = this.users.find(user => user.id === id);
bus.$emit('new_parts', this.parts.data);
console.log(this.parts.data);
},
async fetchUsers(item) {
// Remove in 6 months and say
// you've made optimizations! :)
await pause(1500);
return fetch('http://localhost:8081/api/toppartsdata')
.then(res => res.json())
.then(json => (item.children.push(...json)))
.catch(err => console.warn(err));
},
},
};
import VueHighcharts from 'vue2-highcharts';
import Vue from 'vue';
const bus = new Vue();
const asyncData = {
name: 'Prediction Chart',
marker: {
symbol: 'circle',
},
data: [],
};
export default {
components: {
VueHighcharts,
},
data() {
return {
options: {
chart: {
type: 'spline',
title: '',
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},
yAxis: {
title: {
text: 'LINECOST',
},
labels: {
formatter() {
return `${this.value}°`;
},
},
},
tooltip: {
crosshairs: true,
shared: true,
},
credits: {
enabled: false,
},
plotOptions: {
spline: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1,
},
},
},
series: [],
},
};
},
methods: {
test() {
// eslint-disable-next-line func-names
bus.$on('new_parts', (data) => {
alert(value);
});
},
load() {
// eslint-disable-next-line func-names
bus.$on('new_parts', function (data) {
this.asyncData.data = data;
});
const { lineCharts } = this.$refs;
lineCharts.delegateMethod('showLoading', 'Loading...');
setTimeout(() => {
lineCharts.addSeries(asyncData.data);
lineCharts.hideLoading();
}, 2000);
},
},
};
<template>
<div>
<vue-highcharts :options="options" ref="lineCharts"></vue-highcharts>
</div>
</template>
<script>
import VueHighcharts from 'vue2-highcharts';
import { bus } from '../../../main';
export default {
props: {
partsdata: {
type: Array,
},
},
components: {
VueHighcharts,
},
data() {
return {
options: {
chart: {
type: 'spline',
title: 'Hassaan',
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},
yAxis: {
title: {
text: '',
},
labels: {
formatter() {
return `${this.value}°`;
},
},
},
tooltip: {
crosshairs: true,
shared: true,
},
credits: {
enabled: false,
},
plotOptions: {
spline: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1,
},
},
},
series: [],
},
};
},
created() {
bus.$on('new_user', (data) => { this.series = data; });
},
};
</script>
created() {
bus.$on('new_user', (data) => { this.series = data; });
}
created() {
bus.$on('new_user', (data) => { this.options.series[0].data = data; });
}
我希望能够使用click events from component-A更新我的highcharts时间线图,并在每次单击new按钮时将事件中的数据更新到component-B。如果这是两个组件中使用的真实代码,那么它就不起作用了,因为您正在创建两条不同的总线,而不是在事件中重用同一条总线 尝试在单独的文件(如
bus.js
)中取出它,然后将其导出并导入到需要与之交互的组件中:
// bus.js
export default new Vue()
// Component A
import bus from './bus'
bus.$emit('new_parts', this.parts.data)
// Component B
import bus from './bus'
bus.$on('new_parts', (data) => {
alert(value);
})
如果有什么不合理的地方,请告诉我。如果这是您在两个组件中使用的真实代码,那么它不起作用,因为您正在创建两个不同的总线,而不是重复使用相同的总线来处理事件 尝试在单独的文件(如
bus.js
)中取出它,然后将其导出并导入到需要与之交互的组件中:
// bus.js
export default new Vue()
// Component A
import bus from './bus'
bus.$emit('new_parts', this.parts.data)
// Component B
import bus from './bus'
bus.$on('new_parts', (data) => {
alert(value);
})
如果有什么不合理的地方,请告诉我。最简单的处理方法是使用this.$root发出并侦听事件: 要从组件a发出事件,请执行以下操作:
this.$root.$emit('new_parts', this.parts.data)
要收听组件b上的事件,请执行以下操作:
mounted() {
this.$root.$on('new_parts', (data) => {
//Your code here
});
},
请在mounted方法中添加onclick
下面是一篇关于Vue上事件的好文章:最简单的处理方法是使用this.$root来发出和侦听事件: 要从组件a发出事件,请执行以下操作:
this.$root.$emit('new_parts', this.parts.data)
要收听组件b上的事件,请执行以下操作:
mounted() {
this.$root.$on('new_parts', (data) => {
//Your code here
});
},
请在mounted方法中添加onclick
这是一篇关于Vue事件的好文章:下面是my highcharts组件的更新代码:
import Vue from 'vue';
const bus = new Vue();
const pause = ms => new Promise(resolve => setTimeout(resolve, ms));
export default {
data: () => ({
active: [],
avatar: null,
open: [],
users: [],
}),
computed: {
items() {
return [
{
name: 'Users',
children: this.users,
},
];
},
selected() {
if (!this.active.length) return undefined;
const id = this.active[0];
return this.users.find(user => user.id === id);
},
},
methods: {
fetchData() {
const id = this.active[0];
this.parts = this.users.find(user => user.id === id);
bus.$emit('new_parts', this.parts.data);
console.log(this.parts.data);
},
async fetchUsers(item) {
// Remove in 6 months and say
// you've made optimizations! :)
await pause(1500);
return fetch('http://localhost:8081/api/toppartsdata')
.then(res => res.json())
.then(json => (item.children.push(...json)))
.catch(err => console.warn(err));
},
},
};
import VueHighcharts from 'vue2-highcharts';
import Vue from 'vue';
const bus = new Vue();
const asyncData = {
name: 'Prediction Chart',
marker: {
symbol: 'circle',
},
data: [],
};
export default {
components: {
VueHighcharts,
},
data() {
return {
options: {
chart: {
type: 'spline',
title: '',
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},
yAxis: {
title: {
text: 'LINECOST',
},
labels: {
formatter() {
return `${this.value}°`;
},
},
},
tooltip: {
crosshairs: true,
shared: true,
},
credits: {
enabled: false,
},
plotOptions: {
spline: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1,
},
},
},
series: [],
},
};
},
methods: {
test() {
// eslint-disable-next-line func-names
bus.$on('new_parts', (data) => {
alert(value);
});
},
load() {
// eslint-disable-next-line func-names
bus.$on('new_parts', function (data) {
this.asyncData.data = data;
});
const { lineCharts } = this.$refs;
lineCharts.delegateMethod('showLoading', 'Loading...');
setTimeout(() => {
lineCharts.addSeries(asyncData.data);
lineCharts.hideLoading();
}, 2000);
},
},
};
<template>
<div>
<vue-highcharts :options="options" ref="lineCharts"></vue-highcharts>
</div>
</template>
<script>
import VueHighcharts from 'vue2-highcharts';
import { bus } from '../../../main';
export default {
props: {
partsdata: {
type: Array,
},
},
components: {
VueHighcharts,
},
data() {
return {
options: {
chart: {
type: 'spline',
title: 'Hassaan',
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},
yAxis: {
title: {
text: '',
},
labels: {
formatter() {
return `${this.value}°`;
},
},
},
tooltip: {
crosshairs: true,
shared: true,
},
credits: {
enabled: false,
},
plotOptions: {
spline: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1,
},
},
},
series: [],
},
};
},
created() {
bus.$on('new_user', (data) => { this.series = data; });
},
};
</script>
created() {
bus.$on('new_user', (data) => { this.series = data; });
}
created() {
bus.$on('new_user', (data) => { this.options.series[0].data = data; });
}
从“vue2 highcharts”导入VueHighcharts;
从“../../../main”导入{bus};
导出默认值{
道具:{
partsdata:{
类型:数组,
},
},
组成部分:{
VueHighcharts,
},
数据(){
返回{
选项:{
图表:{
类型:“样条线”,
标题:“哈桑”,
},
xAxis:{
类别:[‘一月’、‘二月’、‘三月’、‘四月’、‘五月’、‘六月’,
“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”],
},
亚克斯:{
标题:{
文本:“”,
},
标签:{
格式化程序(){
返回`${this.value}`;
},
},
},
工具提示:{
十字准星:没错,
分享:是的,
},
学分:{
启用:false,
},
打印选项:{
样条曲线:{
标记:{
半径:4,
线条颜色:'#666666',
线宽:1,
},
},
},
系列:[],
},
};
},
创建(){
总线$on('new_user',(data)=>{this.series=data;});
},
};
下面是my highcharts组件的更新代码:
import Vue from 'vue';
const bus = new Vue();
const pause = ms => new Promise(resolve => setTimeout(resolve, ms));
export default {
data: () => ({
active: [],
avatar: null,
open: [],
users: [],
}),
computed: {
items() {
return [
{
name: 'Users',
children: this.users,
},
];
},
selected() {
if (!this.active.length) return undefined;
const id = this.active[0];
return this.users.find(user => user.id === id);
},
},
methods: {
fetchData() {
const id = this.active[0];
this.parts = this.users.find(user => user.id === id);
bus.$emit('new_parts', this.parts.data);
console.log(this.parts.data);
},
async fetchUsers(item) {
// Remove in 6 months and say
// you've made optimizations! :)
await pause(1500);
return fetch('http://localhost:8081/api/toppartsdata')
.then(res => res.json())
.then(json => (item.children.push(...json)))
.catch(err => console.warn(err));
},
},
};
import VueHighcharts from 'vue2-highcharts';
import Vue from 'vue';
const bus = new Vue();
const asyncData = {
name: 'Prediction Chart',
marker: {
symbol: 'circle',
},
data: [],
};
export default {
components: {
VueHighcharts,
},
data() {
return {
options: {
chart: {
type: 'spline',
title: '',
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},
yAxis: {
title: {
text: 'LINECOST',
},
labels: {
formatter() {
return `${this.value}°`;
},
},
},
tooltip: {
crosshairs: true,
shared: true,
},
credits: {
enabled: false,
},
plotOptions: {
spline: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1,
},
},
},
series: [],
},
};
},
methods: {
test() {
// eslint-disable-next-line func-names
bus.$on('new_parts', (data) => {
alert(value);
});
},
load() {
// eslint-disable-next-line func-names
bus.$on('new_parts', function (data) {
this.asyncData.data = data;
});
const { lineCharts } = this.$refs;
lineCharts.delegateMethod('showLoading', 'Loading...');
setTimeout(() => {
lineCharts.addSeries(asyncData.data);
lineCharts.hideLoading();
}, 2000);
},
},
};
<template>
<div>
<vue-highcharts :options="options" ref="lineCharts"></vue-highcharts>
</div>
</template>
<script>
import VueHighcharts from 'vue2-highcharts';
import { bus } from '../../../main';
export default {
props: {
partsdata: {
type: Array,
},
},
components: {
VueHighcharts,
},
data() {
return {
options: {
chart: {
type: 'spline',
title: 'Hassaan',
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},
yAxis: {
title: {
text: '',
},
labels: {
formatter() {
return `${this.value}°`;
},
},
},
tooltip: {
crosshairs: true,
shared: true,
},
credits: {
enabled: false,
},
plotOptions: {
spline: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1,
},
},
},
series: [],
},
};
},
created() {
bus.$on('new_user', (data) => { this.series = data; });
},
};
</script>
created() {
bus.$on('new_user', (data) => { this.series = data; });
}
created() {
bus.$on('new_user', (data) => { this.options.series[0].data = data; });
}
从“vue2 highcharts”导入VueHighcharts;
从“../../../main”导入{bus};
导出默认值{
道具:{
partsdata:{
类型:数组,
},
},
组成部分:{
VueHighcharts,
},
数据(){
返回{
选项:{
图表:{
类型:“样条线”,
标题:“哈桑”,
},
xAxis:{
类别:[‘一月’、‘二月’、‘三月’、‘四月’、‘五月’、‘六月’,
“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”],
},
亚克斯:{
标题:{
文本:“”,
},
标签:{
格式化程序(){
返回`${this.value}`;
},
},
},
工具提示:{
十字准星:没错,
分享:是的,
},
学分:{
启用:false,
},
打印选项:{
样条曲线:{
标记:{
半径:4,
线条颜色:'#666666',
线宽:1,
},
},
},
系列:[],
},
};
},
创建(){
总线$on('new_user',(data)=>{this.series=data;});
},
};
您的上一个代码几乎正确,但这里有错误的部分:
错误代码:
import Vue from 'vue';
const bus = new Vue();
const pause = ms => new Promise(resolve => setTimeout(resolve, ms));
export default {
data: () => ({
active: [],
avatar: null,
open: [],
users: [],
}),
computed: {
items() {
return [
{
name: 'Users',
children: this.users,
},
];
},
selected() {
if (!this.active.length) return undefined;
const id = this.active[0];
return this.users.find(user => user.id === id);
},
},
methods: {
fetchData() {
const id = this.active[0];
this.parts = this.users.find(user => user.id === id);
bus.$emit('new_parts', this.parts.data);
console.log(this.parts.data);
},
async fetchUsers(item) {
// Remove in 6 months and say
// you've made optimizations! :)
await pause(1500);
return fetch('http://localhost:8081/api/toppartsdata')
.then(res => res.json())
.then(json => (item.children.push(...json)))
.catch(err => console.warn(err));
},
},
};
import VueHighcharts from 'vue2-highcharts';
import Vue from 'vue';
const bus = new Vue();
const asyncData = {
name: 'Prediction Chart',
marker: {
symbol: 'circle',
},
data: [],
};
export default {
components: {
VueHighcharts,
},
data() {
return {
options: {
chart: {
type: 'spline',
title: '',
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},
yAxis: {
title: {
text: 'LINECOST',
},
labels: {
formatter() {
return `${this.value}°`;
},
},
},
tooltip: {
crosshairs: true,
shared: true,
},
credits: {
enabled: false,
},
plotOptions: {
spline: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1,
},
},
},
series: [],
},
};
},
methods: {
test() {
// eslint-disable-next-line func-names
bus.$on('new_parts', (data) => {
alert(value);
});
},
load() {
// eslint-disable-next-line func-names
bus.$on('new_parts', function (data) {
this.asyncData.data = data;
});
const { lineCharts } = this.$refs;
lineCharts.delegateMethod('showLoading', 'Loading...');
setTimeout(() => {
lineCharts.addSeries(asyncData.data);
lineCharts.hideLoading();
}, 2000);
},
},
};
<template>
<div>
<vue-highcharts :options="options" ref="lineCharts"></vue-highcharts>
</div>
</template>
<script>
import VueHighcharts from 'vue2-highcharts';
import { bus } from '../../../main';
export default {
props: {
partsdata: {
type: Array,
},
},
components: {
VueHighcharts,
},
data() {
return {
options: {
chart: {
type: 'spline',
title: 'Hassaan',
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},
yAxis: {
title: {
text: '',
},
labels: {
formatter() {
return `${this.value}°`;
},
},
},
tooltip: {
crosshairs: true,
shared: true,
},
credits: {
enabled: false,
},
plotOptions: {
spline: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1,
},
},
},
series: [],
},
};
},
created() {
bus.$on('new_user', (data) => { this.series = data; });
},
};
</script>
created() {
bus.$on('new_user', (data) => { this.series = data; });
}
created() {
bus.$on('new_user', (data) => { this.options.series[0].data = data; });
}
this.series
不是指系列数据,而是向整个组件对象添加新属性。在您的情况下,它应该是这样的:
正确的代码:
import Vue from 'vue';
const bus = new Vue();
const pause = ms => new Promise(resolve => setTimeout(resolve, ms));
export default {
data: () => ({
active: [],
avatar: null,
open: [],
users: [],
}),
computed: {
items() {
return [
{
name: 'Users',
children: this.users,
},
];
},
selected() {
if (!this.active.length) return undefined;
const id = this.active[0];
return this.users.find(user => user.id === id);
},
},
methods: {
fetchData() {
const id = this.active[0];
this.parts = this.users.find(user => user.id === id);
bus.$emit('new_parts', this.parts.data);
console.log(this.parts.data);
},
async fetchUsers(item) {
// Remove in 6 months and say
// you've made optimizations! :)
await pause(1500);
return fetch('http://localhost:8081/api/toppartsdata')
.then(res => res.json())
.then(json => (item.children.push(...json)))
.catch(err => console.warn(err));
},
},
};
import VueHighcharts from 'vue2-highcharts';
import Vue from 'vue';
const bus = new Vue();
const asyncData = {
name: 'Prediction Chart',
marker: {
symbol: 'circle',
},
data: [],
};
export default {
components: {
VueHighcharts,
},
data() {
return {
options: {
chart: {
type: 'spline',
title: '',
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},
yAxis: {
title: {
text: 'LINECOST',
},
labels: {
formatter() {
return `${this.value}°`;
},
},
},
tooltip: {
crosshairs: true,
shared: true,
},
credits: {
enabled: false,
},
plotOptions: {
spline: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1,
},
},
},
series: [],
},
};
},
methods: {
test() {
// eslint-disable-next-line func-names
bus.$on('new_parts', (data) => {
alert(value);
});
},
load() {
// eslint-disable-next-line func-names
bus.$on('new_parts', function (data) {
this.asyncData.data = data;
});
const { lineCharts } = this.$refs;
lineCharts.delegateMethod('showLoading', 'Loading...');
setTimeout(() => {
lineCharts.addSeries(asyncData.data);
lineCharts.hideLoading();
}, 2000);
},
},
};
<template>
<div>
<vue-highcharts :options="options" ref="lineCharts"></vue-highcharts>
</div>
</template>
<script>
import VueHighcharts from 'vue2-highcharts';
import { bus } from '../../../main';
export default {
props: {
partsdata: {
type: Array,
},
},
components: {
VueHighcharts,
},
data() {
return {
options: {
chart: {
type: 'spline',
title: 'Hassaan',
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},
yAxis: {
title: {
text: '',
},
labels: {
formatter() {
return `${this.value}°`;
},
},
},
tooltip: {
crosshairs: true,
shared: true,
},
credits: {
enabled: false,
},
plotOptions: {
spline: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1,
},
},
},
series: [],
},
};
},
created() {
bus.$on('new_user', (data) => { this.series = data; });
},
};
</script>
created() {
bus.$on('new_user', (data) => { this.series = data; });
}
created() {
bus.$on('new_user', (data) => { this.options.series[0].data = data; });
}
我使用highcharts vue我推荐的vue官方highcharts包装器为您准备了一个在线示例。在那里,您可以找到组件间通信的工作代码
演示:您的上一个代码几乎是正确的,但这是错误的部分: 错误代码:
import Vue from 'vue';
const bus = new Vue();
const pause = ms => new Promise(resolve => setTimeout(resolve, ms));
export default {
data: () => ({
active: [],
avatar: null,
open: [],
users: [],
}),
computed: {
items() {
return [
{
name: 'Users',
children: this.users,
},
];
},
selected() {
if (!this.active.length) return undefined;
const id = this.active[0];
return this.users.find(user => user.id === id);
},
},
methods: {
fetchData() {
const id = this.active[0];
this.parts = this.users.find(user => user.id === id);
bus.$emit('new_parts', this.parts.data);
console.log(this.parts.data);
},
async fetchUsers(item) {
// Remove in 6 months and say
// you've made optimizations! :)
await pause(1500);
return fetch('http://localhost:8081/api/toppartsdata')
.then(res => res.json())
.then(json => (item.children.push(...json)))
.catch(err => console.warn(err));
},
},
};
import VueHighcharts from 'vue2-highcharts';
import Vue from 'vue';
const bus = new Vue();
const asyncData = {
name: 'Prediction Chart',
marker: {
symbol: 'circle',
},
data: [],
};
export default {
components: {
VueHighcharts,
},
data() {
return {
options: {
chart: {
type: 'spline',
title: '',
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},
yAxis: {
title: {
text: 'LINECOST',
},
labels: {
formatter() {
return `${this.value}°`;
},
},
},
tooltip: {
crosshairs: true,
shared: true,
},
credits: {
enabled: false,
},
plotOptions: {
spline: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1,
},
},
},
series: [],
},
};
},
methods: {
test() {
// eslint-disable-next-line func-names
bus.$on('new_parts', (data) => {
alert(value);
});
},
load() {
// eslint-disable-next-line func-names
bus.$on('new_parts', function (data) {
this.asyncData.data = data;
});
const { lineCharts } = this.$refs;
lineCharts.delegateMethod('showLoading', 'Loading...');
setTimeout(() => {
lineCharts.addSeries(asyncData.data);
lineCharts.hideLoading();
}, 2000);
},
},
};
<template>
<div>
<vue-highcharts :options="options" ref="lineCharts"></vue-highcharts>
</div>
</template>
<script>
import VueHighcharts from 'vue2-highcharts';
import { bus } from '../../../main';
export default {
props: {
partsdata: {
type: Array,
},
},
components: {
VueHighcharts,
},
data() {
return {
options: {
chart: {
type: 'spline',
title: 'Hassaan',
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},
yAxis: {
title: {
text: '',
},
labels: {
formatter() {
return `${this.value}°`;
},
},
},
tooltip: {
crosshairs: true,
shared: true,
},
credits: {
enabled: false,
},
plotOptions: {
spline: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1,
},
},
},
series: [],
},
};
},
created() {
bus.$on('new_user', (data) => { this.series = data; });
},
};
</script>
created() {
bus.$on('new_user', (data) => { this.series = data; });
}
created() {
bus.$on('new_user', (data) => { this.options.series[0].data = data; });
}
this.series
不是指系列数据,而是向整个组件对象添加新属性。在您的情况下,它应该是这样的:
正确的代码:
import Vue from 'vue';
const bus = new Vue();
const pause = ms => new Promise(resolve => setTimeout(resolve, ms));
export default {
data: () => ({
active: [],
avatar: null,
open: [],
users: [],
}),
computed: {
items() {
return [
{
name: 'Users',
children: this.users,
},
];
},
selected() {
if (!this.active.length) return undefined;
const id = this.active[0];
return this.users.find(user => user.id === id);
},
},
methods: {
fetchData() {
const id = this.active[0];
this.parts = this.users.find(user => user.id === id);
bus.$emit('new_parts', this.parts.data);
console.log(this.parts.data);
},
async fetchUsers(item) {
// Remove in 6 months and say
// you've made optimizations! :)
await pause(1500);
return fetch('http://localhost:8081/api/toppartsdata')
.then(res => res.json())
.then(json => (item.children.push(...json)))
.catch(err => console.warn(err));
},
},
};
import VueHighcharts from 'vue2-highcharts';
import Vue from 'vue';
const bus = new Vue();
const asyncData = {
name: 'Prediction Chart',
marker: {
symbol: 'circle',
},
data: [],
};
export default {
components: {
VueHighcharts,
},
data() {
return {
options: {
chart: {
type: 'spline',
title: '',
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},
yAxis: {
title: {
text: 'LINECOST',
},
labels: {
formatter() {
return `${this.value}°`;
},
},
},
tooltip: {
crosshairs: true,
shared: true,
},
credits: {
enabled: false,
},
plotOptions: {
spline: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1,
},
},
},
series: [],
},
};
},
methods: {
test() {
// eslint-disable-next-line func-names
bus.$on('new_parts', (data) => {
alert(value);
});
},
load() {
// eslint-disable-next-line func-names
bus.$on('new_parts', function (data) {
this.asyncData.data = data;
});
const { lineCharts } = this.$refs;
lineCharts.delegateMethod('showLoading', 'Loading...');
setTimeout(() => {
lineCharts.addSeries(asyncData.data);
lineCharts.hideLoading();
}, 2000);
},
},
};
<template>
<div>
<vue-highcharts :options="options" ref="lineCharts"></vue-highcharts>
</div>
</template>
<script>
import VueHighcharts from 'vue2-highcharts';
import { bus } from '../../../main';
export default {
props: {
partsdata: {
type: Array,
},
},
components: {
VueHighcharts,
},
data() {
return {
options: {
chart: {
type: 'spline',
title: 'Hassaan',
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},
yAxis: {
title: {
text: '',
},
labels: {
formatter() {
return `${this.value}°`;
},
},
},
tooltip: {
crosshairs: true,
shared: true,
},
credits: {
enabled: false,
},
plotOptions: {
spline: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1,
},
},
},
series: [],
},
};
},
created() {
bus.$on('new_user', (data) => { this.series = data; });
},
};
</script>
created() {
bus.$on('new_user', (data) => { this.series = data; });
}
created() {
bus.$on('new_user', (data) => { this.options.series[0].data = data; });
}
我使用highcharts vue我推荐的vue官方highcharts包装器为您准备了一个在线示例。在那里,您可以找到组件间通信的工作代码
演示:我认为该代码不起作用,因为组件A处的
bus
和组件B处的bus
是不同的。您应该从父组件捕获事件并执行逻辑。我认为此代码不起作用,因为组件A的bus
和组件B的bus
是不同的。您应该从父组件捕获事件并执行逻辑。我遵循了您的示例,但现在单击并发出事件时highcharts没有更新。我遵循了您的Exampl