这是我昨天提出的一个问题。 Truncate Text in an array of object?
我想应用相同的逻辑,但是要使用我的vuex存储。我的商店看起来像这样:-
state: {
Data: []
},getters: {
getData:state => {
const maxLength = 10;
const resultArray = state.Data.map(i => {
if (i.name <= maxLength) i.shortName = i.name;
else {
const shortenedName = i.name.substring(0,maxLength + 1);
i.shortName = shortenedName + '...';
}
return i;
});
console.log(resultArray)
}
},mutations: {
setData(state,payload) {
state.Data = payload
},const actions = {
setItems: (context,payload) => {
if (payload) {
context.commit("setData",payload);
return;
}
else {
Vue.$http.get(`https://example.com/getData`).then(
(response) => {
context.commit("setData",response.body);
},(error) => {
console.log(error);
}
);
}
}
}
因此,我不确定如何在我的getter或setter上应用截断逻辑,它们将返回所需的数组,可以使用{{mapMutation}}
或{{mapGetter}}
在我的组件中使用。
任何帮助将不胜感激。非常感谢。