computed不用加()

computed: {
    goodsId () {
      return this.$route.params.id
    }
  },
  created () {
    this.getDetail()
  },
  methods: {
    onChange (index) {
      this.current = index
    },
    async getDetail () {
      const res = await getProDetail(this.goodsId)
      console.log(res)
    }
  }

为什么不写成const res = await getProDetail(this.goodsId()),goodsId不是一个函数吗

根据你提供的代码,goodsId是一个计算属性,而不是一个函数。计算属性在Vue中用于根据依赖的数据动态计算出一个新的值。

created生命周期钩子中,通过调用this.getDetail()方法来获取商品详情。在getDetail方法中,你使用了this.goodsId来作为参数传递给getProDetail函数。

在这里,this.goodsId不是一个函数,而是一个计算属性。计算属性会自动计算并返回一个新的值,而不需要显式调用。所以在getDetail方法中,你直接使用this.goodsId而不需要加上括号。

修改代码如下:

computed: {
  goodsId() {
    return this.$route.params.id;
  }
},
created() {
  this.getDetail();
},
methods: {
  onChange(index) {
    this.current = index;
  },
  async getDetail() {
    const res = await getProDetail(this.goodsId);
    console.log(res);
  }
}

getDetail方法中,你可以直接使用this.goodsId来获取计算属性的值,而无需将其作为函数调用。

希望以上解释对你有帮助。如果还有其他问题,请随时提问。

你可能感兴趣的:(vue.js)