【发布时间】:2020-12-19 19:09:09
【问题描述】:
我使用模板获取json文件的数据,我使用“v-for”打印所有数据,例如:
template: /*html*/
`
<div class="col-lg-8">
<template v-for="item of actividades">
<ul>
<li>{{ item.date }}</li>
<ul>
</template>
</div>
`,
但我需要使用函数 year() 来修改这些信息并返回和结果,例如:
template: /*html*/
`
<div class="col-lg-8">
<template v-for="item of actividades">
<ul>
<li>{{ year(item.date) }}</li>
<ul>
</template>
</div>
`,
值 {{ item.date }} 打印“2021-01-20”,但我希望使用函数 {{ year(item.date) }}
使用javascript编写函数year():
year(date){
return String(date).substr(0, 4);
}
我尝试使用该代码但无法正常工作,出现此错误:
这是我的 javascript 代码:
//VueEx
const store = new Vuex.Store({
state: {
actividades: [],
programas: [],
year: ""
},
mutations: {
llamarJsonMutation(state, llamarJsonAction){
state.actividades = llamarJsonAction.Nueva_estructura_proveedor;
state.programas = llamarJsonAction.BD_programas;
},
yearFunction(state, date){
state.year = String(date).substr(8, 2);
return state.year;
}
},
actions: {
llamarJson: async function({ commit }){
const data = await fetch('calendario-2021-prueba.json');
const dataJson = await data.json();
commit('llamarJsonMutation', dataJson);
}
}
});
//Vue
new Vue({
el: '#caja-vue',
store: store,
created() {
this.$store.dispatch('llamarJson');
}
});
【问题讨论】:
-
它怎么不“工作”?预期的结果是什么?究竟发生了什么?
-
出现循环错误。
-
错误信息是什么?
-
您关于循环数据并需要
year函数的组件的问题的第一部分,关于 vuex 存储的第二部分,但您没有展示这两者如何交互。真的很难理解你的问题。 -
我不确定是否允许使用
{{}}中的函数。无论如何,我认为您可以在分配给 actividades 之前处理函数 llamarJsonMutation 中的数据。
标签: javascript vue.js vuex