在构建包含名为 fabmoment 的 3D 打印信息的组件时,我成功地使用 $route.params 填充了作者对象和 fabmoment 对象的信息,像这样:

<script> 
import SingleSummarized from './SingleSummarized' 
// import Comments from '@/components/Comments/Multiple' 
export default { 
  name: 'Single', 
  data () { 
    return { 
      author: null, 
      fabmoment: null, 
      tempLicenseID: null, 
      license: null 
    } 
  }, 
  created () { 
    this.$http.get(`/users/${this.$route.params.author_id}`) 
        .then(request => { this.author = request.data }) 
        .catch(() => { alert('Something went wrong when trying to retrieve this user!') }) 
    this.$http.get(`/users/${this.$route.params.author_id}/fabmoments/${this.$route.params.fabmoment_id}`) 
        .then(request => { this.fabmoment = request.data }) 
        .catch(() => { alert('Something went wrong when trying to retrieve the fabmoment attribute data!') }) 
    this.$http.get(`/licenses/${this.fabmoment.license_id`) 
        .then(request => { this.license = request.data }) 
        .catch(() => { alert('Something went wrong when trying to retrieve the license!') }) 
  }, 
  components: { 
    SingleSummarized 
    // Comments 
  } 
} 
</script> 

在创建的部分中,您可以看到我还尝试使用 this.fabmoment.license_id 检索 fabmoment 的许可证。它在这条线上失败了。

tempLicenseID 是一个想法吗?因为我怀疑 this.fabmoment 尚不可用。我将如何使用它来使请求起作用?或者还有其他更明智的解决方案吗?

请您参考如下方法:

您必须在检索 fabmoment 对象后请求许可证:

this.$http.get(`/users/${this.$route.params.author_id}/fabmoments/${this.$route.params.fabmoment_id}`) 
    .then(request => { return this.fabmoment = request.data }) 
    .then( fabmoment => this.$http.get(`/licenses/${fabmoment.license_id`) 
    .then(request => { this.license = request.data }) 
    .catch(() => { alert('Something went wrong when trying to retrieve the fabmoment attribute data!') }) 


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!