Vue.js Vue和来自RESTAPI的数据

Vue.js Vue和来自RESTAPI的数据,vue.js,Vue.js,在使用vue.js时,我注意到一些奇怪的行为,试图在页面上显示来自API的数据,但奇怪的是: 使用vue 2.0.0,我可以看到“标题”,但在开发人员控制台中有一个错误[请参阅printscreen] 使用最新的vue版本,我看不到“标题”[我在printscreen中也有同样的错误] 这是正常的还是正常的 源代码: template: '<div>'+ 'Form with id = {{id}}'+ '<br/>'+

在使用vue.js时,我注意到一些奇怪的行为,试图在页面上显示来自API的数据,但奇怪的是:

  • 使用vue 2.0.0,我可以看到“标题”,但在开发人员控制台中有一个错误[请参阅printscreen]
  • 使用最新的vue版本,我看不到“标题”[我在printscreen中也有同样的错误]
这是正常的还是正常的

源代码:

  template:
    '<div>'+
      'Form with id = {{id}}'+
      '<br/>'+
      'has title = {{item.details.Title}}'+
    '</div>',
  data: function(){
    return {
      id: '',
      item: {}
    }
  },
  created: function() {
    this.get()
  },  
  methods: {
    get: function() {
      var self = this
      id = window.location.hash
      id = id.replace('#/whatever/','')
      axiosInstance.get('/thebackendresource/'+id) // <--- make http calls etc
        .then(function (response) {
          self.id = id
          self.item = response.data
          console.log(self.item)
        }).catch(function (error) {
          console.log(error)
        }
      );
    }
  } 
模板:
''+
'id为{id}的窗体'+
“
”+ 'has title={{item.details.title}}'+ '', 数据:函数(){ 返回{ id:“”, 项目:{} } }, 已创建:函数(){ 这个 }, 方法:{ get:function(){ var self=这个 id=window.location.hash id=id.replace(“#/which/”,“”)
axiosInstance.get('/thebackendresource/'+id)/您将收到此错误,因为当您从
axiosInstance
获取数据时,该时间
项.details
为空,并且当它尝试呈现时会抛出此错误

api调用完成后,它会更新DOM的属性,然后重新呈现DOM,这样您就可以看到
item.details.Title
rendered

您需要添加一个空检查以防止此错误,这可以通过以下方式轻松完成:

template:
'<div>'+
  'Form with id = {{id}}'+
  '<br/>'+
  '<span v-if="item.details"> has title = {{item.details.Title}}'+
  '</span>' +
'</div>',
模板:
''+
'id为{id}的窗体'+
“
”+ 'has title={{item.details.title}}'+ '' + '',
您将收到此错误,因为当您从
axiosinstance
提取数据时,该时间
项.details
为空,并且当它尝试渲染时会抛出此错误

api调用完成后,它会更新DOM的属性,然后重新呈现DOM,这样您就可以看到
item.details.Title
rendered

您需要添加一个空检查以防止此错误,这可以通过以下方式轻松完成:

template:
'<div>'+
  'Form with id = {{id}}'+
  '<br/>'+
  '<span v-if="item.details"> has title = {{item.details.Title}}'+
  '</span>' +
'</div>',
模板:
''+
'id为{id}的窗体'+
“
”+ 'has title={{item.details.title}}'+ '' + '',
我希望我可以从组件生命周期中调整此行为,它正在工作我希望我可以从组件生命周期中调整此行为,它正在工作