首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在挂载钩子中而不是在单个方法中初始化代码?

如何在挂载钩子中而不是在单个方法中初始化代码?
EN

Stack Overflow用户
提问于 2020-06-24 13:00:55
回答 1查看 35关注 0票数 0

我正在学习如何使用名为安诺里奥的Javascript图像注释库。如何在挂载钩子期间初始化库(假设这是实现它的最佳位置),而不是像我现在使用的方式那样用独立的方法初始化库?

我有两种方法:(working 这里 )

PhotoDetail.vue

代码语言:javascript
复制
annotatePhoto() {
  try {
    const anno = new Annotorious({   <-------- how to get this out of this scope and into the global scope of this SFC?
      image: this.photo.id // image element or ID
    })
    // Attach listeners to handle annotation events
    anno.on('createAnnotation', annotation => {
      console.log('Created!', annotation)
    })
  } catch (error) {
    console.log('anno error', error)
  }
}

代码语言:javascript
复制
loadAnnotations() {
const anno = new Annotorious({   <-------- how to get this out of this scope and into the global scope of this SFC?
      image: this.photo.id // image element or ID
    })
          anno.setAnnotations(annotations) < --- this is undefined because of this scope
        })
    }

我试过:

代码语言:javascript
复制
  computed: {
    anno() {
      const anno = Annotorious.init({
        image: this.photo.id
      })
      return anno
    }
  }

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2020-06-24 17:13:30

我做了些什么让这件事起作用:

代码语言:javascript
复制
  data() {
    return {
      photo: {},
      anno: {}
    }
  },
  async mounted() {
    await this.getPhoto()
    this.anno = new Annotorious({ image: this.photo.id })
    this.annotatePhoto()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62555670

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档