错误代码

export const getCollectedArticleListAPI = ({ page = 1, per_page = 10 }) => {
  return request({
    url: '/v1_0/article/collections',
    method: 'GET',
    params: {
      page,
      per_page
    }
  })
}

 报错代码

vue.runtime.esm.js?c320:4560 [Vue warn]: Error in created hook (Promise/async): "TypeError: Cannot read properties of undefined (reading 'page')"

found in

---> <CommentList>
       <ArticleDetail>
         <App> at src/App.vue
           <Root>

 

 改了传参方式 发现可以 (调用的时候都没有传参)

export const getCollectedArticleListAPI = () => {
  return request({
    url: '/v1_0/article/collections',
    method: 'GET',
    params: {
      page:1,
      per_page:10
    }
  })
}

在网上找了很久找到了原因

JS函数默认参数为对象,调用时不传参报错

正确写法

export const getCollectedArticleListAPI = ({ page = 1, per_page = 10 } = {}) => {
  return request({
    url: '/v1_0/article/collections',
    method: 'GET',
    params: {
      page,
      per_page
    }
  })
}

 

错误传参和正确传参对比

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐