简体   繁体   English

Vue.js - 正确使用多个 Vue 实例?

[英]Vue.js - proper use of multiple Vue instances?

My question is somewhat related to this one , however, it is not apparent to me to what extent the described approach is actively discouraged by Vue.我的问题与这个有点相关,但是,我不清楚 Vue 在多大程度上积极阻止了所描述的方法。

In my case I face the following scenario: I work on a project where the DOM is provided entirely by the php backend per twig templates and is intended to be enriched with further business logic on the frontend via Vue.在我的情况下,我面临以下情况:我在一个项目中工作,其中 DOM 完全由 php 后端每个 twig 模板提供,并且旨在通过 Vue 在前端进一步丰富业务逻辑。

A minimal twig-less sketch can be found here可以在这里找到一个最小的无树枝草图

I understand that I lose out on the reactivity feature of Vue.js by creating a new Vue instance for each of the DOM elements in question.我知道通过为每个有问题的 DOM 元素创建一个新的 Vue 实例,我失去了 Vue.js 的反应性功能。 In the current context, it is a tradeoff I can live with as there is no dependency on cross-component state updates.在当前情况下,这是我可以接受的折衷方案,因为不依赖于跨组件 state 更新。 However, is it an absolute antipattern to use Vue in this form, ie multiple Vue instances instead of one parent instance?但是,以这种形式使用 Vue 是否是一种绝对的反模式,即多个 Vue 实例而不是一个父实例?

  1. You only miss out on reactivity between components, not within components.您只会错过组件之间的反应性,而不是组件内的反应性。
  2. You can still have reactivity between components by using an additional shared Vue instance or shared reactive state.您仍然可以通过使用额外的共享 Vue 实例或共享响应式 state 在组件之间进行响应式。

It is not an antipattern if isolation is your goal.如果隔离是您的目标,这不是反模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM