Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

javascript - 在方法运行时创建Vue组件/元素(Vue component/element creating while method is working)

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元素?)

  ask by Micha? Kruk translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

(方法工作时是否可以创建组件或html元素?)

Its not possible in JavaScript using synchronous code .

(在JavaScript中, 使用同步代码是不可能的。)

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)

(当您执行同步/长时间运行的代码时,JavaScript是单线程的,甚至浏览器渲染都被阻止,更不用说Vue逻辑可以重新渲染您的模板并更新DOM( 真的推荐此演讲 -很好地解释了问题))

You have basically two options:

(您基本上有两个选择:)

  1. Split up you workload into smaller chunks, process only one at a time and use setTimeout(nextBatch, 0) to schedule "next chunk" processing.

    (将您的工作负载分成较小的块,一次仅处理一个,然后使用setTimeout(nextBatch, 0)安排“下一个块”处理。)

    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)

    (您可以将计算工作卸载到WebWorker ,该WebWorker在其自己的线程中运行,但是带来了新的挑战(例如,您的Vue应用程序和Web Worker之间的数据需要在两侧进行序列化/反序列化))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...