更新Nuxt中由asyncData()获取的数据

时间:2019-03-08 21:48:20

标签: nuxt.js

我想知道是否有一种方法可以更新asyncData()返回的数据吗?对于论坛应用程序,在帖子页面中,我通过以下方式获得了回复数据:

async asyncData(){
    const {data:replies}=await axios.get('replies.json')
    return { replies }
}

并将其呈现在模板中:

<div v-for="rep in replies">
    <div>{{rep.likes}}</div>
    <div>{{rep.content}}</div>
</div>

我想在帖子页面中实现replyToPost功能。但是,当用户发布回复时,我不知道如何更新已经通过replies方法获取的asyncData()数据。

1 个答案:

答案 0 :(得分:0)

任何asyncData项,由于Nuxt.js will automatically merge the returned object with the component data.初始化后,在您的情况下,“回复”的行为就像常规数据项一样。因此,您可以像其他任何数据项一样更改它。我假设“ replies”是一个数组,所以您只需一个方法即可:

      df <- structure(list(year = c(2022, 2023, 2024, 2025, 2026, 2027, 2028, 
2029, 2030, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030, 
2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030, 2022, 2023, 
2024), fuel = c("Jet.Fuel", "Jet.Fuel", "Jet.Fuel", "Jet.Fuel", 
"Jet.Fuel", "Jet.Fuel", "Jet.Fuel", "Jet.Fuel", "Jet.Fuel", "Diesel", 
"Diesel", "Diesel", "Diesel", "Diesel", "Diesel", "Diesel", "Diesel", 
"Diesel", "Heavy.Fuel.Oil", "Heavy.Fuel.Oil", "Heavy.Fuel.Oil", 
"Heavy.Fuel.Oil", "Heavy.Fuel.Oil", "Heavy.Fuel.Oil", "Heavy.Fuel.Oil", 
"Heavy.Fuel.Oil", "Heavy.Fuel.Oil", "Jet.Fuel", "Jet.Fuel", "Jet.Fuel"
), energy = c(8655.41240337457, 8958.34979088816, 9272.07896169514, 
9596.98460984862, 9933.46514289147, 10281.9331707122, 10642.8160118277, 
11016.5562177133, 11403.6121158249, 826.938198253626, 806.269551838661, 
786.117501398496, 766.469135037703, 747.311863583156, 728.633412517855, 
710.421814116347, 692.665399776729, 675.35279254429, 688.388573194762, 
671.182861757664, 654.407193057451, 638.050818526345, 622.103258248261, 
606.554294244076, 591.39396392473, 576.612553707958, 562.200592794569, 
8655.41240337457, 8958.34979088816, 9272.07896169514), sector = c("Aviation", 
"Aviation", "Aviation", "Aviation", "Aviation", "Aviation", "Aviation", 
"Aviation", "Aviation", "Marine", "Marine", "Marine", "Marine", 
"Marine", "Marine", "Marine", "Marine", "Marine", "Marine", "Marine", 
"Marine", "Marine", "Marine", "Marine", "Marine", "Marine", "Marine", 
"Aviation", "Aviation", "Aviation"), costs.10.50 = c(0.01736, 
0.0347200000000001, 0.0520800000000001, 0.0868000000000002, 0.12152, 
0.1736, 0.22568, 0.27776, 0.3472, 0.0179324999999999, 0.0358649999999997, 
0.0537975000000003, 0.0896625, 0.1255275, 0.179325, 0.2331225, 
0.28692, 0.35865, 0.0187925, 0.037585, 0.0563775, 0.0939624999999999, 
0.1315475, 0.187925, 0.2443025, 0.30068, 0.37585, 0.01736, 0.0347200000000001, 
0.0520800000000001), costs.10.200 = c(0.0694400000000002, 0.13888, 
0.208320000000001, 0.347200000000001, 0.486080000000001, 0.694400000000002, 
0.902719999999999, 1.11104, 1.3888, 0.0717299999999994, 0.143459999999999, 
0.215190000000001, 0.35865, 0.502110000000002, 0.7173, 0.932489999999999, 
1.14768, 1.4346, 0.07517, 0.15034, 0.22551, 0.37585, 0.52619, 
0.7517, 0.977209999999999, 1.20272, 1.5034, 0.0694400000000002, 
0.13888, 0.208320000000001), costs.30.50 = c(0.0572879999999998, 
0.114576, 0.171864, 0.28644, 0.401016, 0.57288, 0.744744, 0.916608, 
1.0416, 0.0591772499999998, 0.1183545, 0.17753175, 0.29588625, 
0.41424075, 0.5917725, 0.76930425, 0.946836, 1.07595, 0.0620152499999996, 
0.1240305, 0.18604575, 0.31007625, 0.43410675, 0.6201525, 0.80619825, 
0.992244, 1.12755, 0.0572879999999998, 0.114576, 0.171864), costs.30.200 = c(0.229151999999999, 
0.458304000000001, 0.687456, 1.14576, 1.604064, 2.29152, 2.978976, 
3.666432, 4.1664, 0.236708999999999, 0.473418000000001, 0.710127, 
1.183545, 1.656963, 2.36709, 3.077217, 3.787344, 4.3038, 0.248060999999998, 
0.496122, 0.744183000000001, 1.240305, 1.736427, 2.48061, 3.224793, 
3.968976, 4.5102, 0.229151999999999, 0.458304000000001, 0.687456
), credit.price = c("Credit Price= $50 per Tonne \n %10 CI Reduction", 
"Credit Price= $50 per Tonne \n %10 CI Reduction", "Credit Price= $50 per Tonne \n %10 CI Reduction", 
"Credit Price= $50 per Tonne \n %10 CI Reduction", "Credit Price= $50 per Tonne \n %10 CI Reduction", 
"Credit Price= $50 per Tonne \n %10 CI Reduction", "Credit Price= $50 per Tonne \n %10 CI Reduction", 
"Credit Price= $50 per Tonne \n %10 CI Reduction", "Credit Price= $50 per Tonne \n %10 CI Reduction", 
"Credit Price= $50 per Tonne \n %10 CI Reduction", "Credit Price= $50 per Tonne \n %10 CI Reduction", 
"Credit Price= $50 per Tonne \n %10 CI Reduction", "Credit Price= $50 per Tonne \n %10 CI Reduction", 
"Credit Price= $50 per Tonne \n %10 CI Reduction", "Credit Price= $50 per Tonne \n %10 CI Reduction", 
"Credit Price= $50 per Tonne \n %10 CI Reduction", "Credit Price= $50 per Tonne \n %10 CI Reduction", 
"Credit Price= $50 per Tonne \n %10 CI Reduction", "Credit Price= $50 per Tonne \n %10 CI Reduction", 
"Credit Price= $50 per Tonne \n %10 CI Reduction", "Credit Price= $50 per Tonne \n %10 CI Reduction", 
"Credit Price= $50 per Tonne \n %10 CI Reduction", "Credit Price= $50 per Tonne \n %10 CI Reduction", 
"Credit Price= $50 per Tonne \n %10 CI Reduction", "Credit Price= $50 per Tonne \n %10 CI Reduction", 
"Credit Price= $50 per Tonne \n %10 CI Reduction", "Credit Price= $50 per Tonne \n %10 CI Reduction", 
"Credit Price= $200 per Tonne \n %10 CI Reduction", "Credit Price= $200 per Tonne \n %10 CI Reduction", 
"Credit Price= $200 per Tonne \n %10 CI Reduction"), costs.total = c(5.52949290307105, 
11.4460476944187, 17.7703473015631, 30.6550721201629, 44.4218203772416, 
65.6859244224319, 88.3888424058135, 112.60647850518, 145.703495859409, 
0.566470444974992, 1.10462395560969, 1.61552216995276, 2.62523918293615, 
3.58347285631672, 4.991295531931, 6.32650481760311, 7.59185105845047, 
9.25262365955757, 0.527810924279912, 1.0292374406538, 1.50526873428513, 
2.44607628145989, 3.33891403726805, 4.65065800242937, 5.89474417396623, 
7.07373519607548, 8.62116618631502, 22.1179716122842, 45.7841907776746, 
71.0813892062524)), row.names = c(NA, 30L), class = "data.frame")

,它将被添加到数组中。