在方法运行时创建Vue组件/元素 [英] Vue component/element creating while method is working

查看:144
本文介绍了在方法运行时创建Vue组件/元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的组件内部有一个解析器方法,当该方法运行时,我想从计算出的值中创建组件(甚至只是元素).方法运行时是否可以创建组件或html元素?

I have a parser method inside my component, when that method is working I want to create components (or even just elements) from values that is computed. Is it possible to creating component or html elements when method is working?

推荐答案

在方法运行时是否可以创建组件或html元素?

Is it possible to creating component or html elements when method is working?

在JavaScript 使用同步代码中是不可能的.当您执行同步/长时间运行的代码时,JavaScript是单线程的,甚至阻止了浏览器渲染,更不用说Vue逻辑了,它重新渲染了模板并更新了DOM(

Its not possible in JavaScript using synchronous code. JavaScript is single-threaded and even browser rendering is blocked when your synchronous/long running code is executing, not to mention Vue logic which re-renders your template and updates the DOM (really recommend this talk - explains the problem beautifully)

您基本上有两种选择:

  1. 将工作负载分成较小的块,一次仅处理一个,然后使用setTimeout(nextBatch, 0)安排下一个块"处理.有关更多详细信息,请参见此SO问题.
  2. 您可以将计算卸载到 WebWorker 它运行在自己的线程中,但是带来了新的挑战(例如,您的Vue应用程序和Web Worker之间的数据都需要在两侧进行序列化/反序列化)
  1. Split up you workload into smaller chunks, process only one at a time and use setTimeout(nextBatch, 0) to schedule "next chunk" processing. See this SO question for more details...
  2. You can offload your computation to WebWorker which is running in its own thread but brings new challenges (for example data between your Vue app and Web Worker need to be serialized/deserialized on both sides)

这篇关于在方法运行时创建Vue组件/元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆