Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript VueJS:显示从computed属性返回的PromiseResult的内容_Javascript_Vue.js_Promise_Computed Properties - Fatal编程技术网

Javascript VueJS:显示从computed属性返回的PromiseResult的内容

Javascript VueJS:显示从computed属性返回的PromiseResult的内容,javascript,vue.js,promise,computed-properties,Javascript,Vue.js,Promise,Computed Properties,我计算了如下所示的属性,它返回res,其中res是一个承诺对象。我无法将此脚本放入created()或mounted()钩子中的原因是this.selectedObject在执行此操作时为null 我对res很满意,但是,当我需要在html中呈现PromiseResult时,问题就出现了:{{currentDepStatus}。这将显示[Promise object],而不是显示PromiseResult的内容。任何帮助都将不胜感激 computed: { currentDepSta

我计算了如下所示的属性,它返回
res
,其中
res
是一个承诺对象。我无法将此脚本放入
created()
mounted()
钩子中的原因是
this.selectedObject
在执行此操作时为
null

我对
res
很满意,但是,当我需要在html中呈现
PromiseResult
时,问题就出现了:
{{currentDepStatus}
。这将显示
[Promise object]
,而不是显示PromiseResult的内容。任何帮助都将不胜感激

  computed: {
    currentDepStatus() {
      let res = '';
      if (!this.selectedObject) return [];
      const deps = this.depCategory(this.selectedObject.id);
      if (deps.length > 0) {
        res = sendDepStatus(deps);
      }
      return res;
    },

这是一个反模式,用于说明如何执行此操作<代码>计算的属性需要为嵌套的被动值简化
getter
。任何外部操作都应该在
方法内部

在看到你的问题后,我会这样做

data() {
  currentDepStatus: []
}
watch: {
 async selectedObject() {
   if (this.selectedObject) {
      const deps = this.depCategory(this.selectedObject.id);
      if (deps.length > 0) {
        this.currentDepStatus = await sendDepStatus(deps);
      }
    }
  }
}

<div>{{ currentDepStatus }}</div>
data(){
currentDepStatus:[]
}
观察:{
异步selectedObject(){
如果(此.selectedObject){
const deps=this.depCategory(this.selectedObject.id);
如果(深度长度>0){
this.currentDepStatus=等待发送DepStatus(deps);
}
}
}
}
{{currentDepStatus}}