当从箭头函数返回一个对象时,由于语法的模糊性,似乎有必要使用额外的一组{}和一个return关键字.
这意味着我不能写p => {foo: "bar"},但必须写p => { return {foo: "bar"}; }.
如果箭头函数返回除对象以外的任何内容{},return则不需要,例如:p => "foo".
p => {foo: "bar"}回报undefined.
修改后的p => {"foo": "bar"}抛出" SyntaxError:意外标记:' :'".
有什么明显的东西我不见了吗?
有一个v-select组件,在更改时,我启动了fillData(selected)选定的位置v-model。而且我需要datacollection.datasets.label在更改时更新标签。我怎么做 ?
<script>
import BarChart from './BarChart.js'
import { mapGetters, mapActions } from "vuex";
export default {
name : "TestLegPerformance",
components: {
BarChart
},
data: () => ({
datacollection : {
labels: ['Week-1','Week-2','Week-3'],
datasets: [
{
label: '',
backgroundColor: '#C58917',
data: [40, 50, 20]
}
]
},
selected: []
}),
computed: {
...mapGetters({
planNames: "planNames"
})
},
mounted () {
this.getAllPlanNamesAction();
},
methods: {
...mapActions(["getAllPlanNamesAction"]),
fillData(selected){
console.log(selected)
},
}
}
</script>
Run Code Online (Sandbox Code Playgroud) 我知道 vue 期望 data 是一个返回对象的函数,所以:
data () {
return {}
}
Run Code Online (Sandbox Code Playgroud)
有效,但这不起作用:
data: () => {
}
Run Code Online (Sandbox Code Playgroud)
为什么它不起作用?不是'它们都是返回一个对象的相同函数