简体   繁体   English

如何在一个Vue组件中每页仅运行一次代码(当同一页面中存在多个相同组件时)?

[英]How does one run code in a Vue component only once per page (when more than one of the same component exists on that page)?

So I have a Vue component which has two functions. 所以我有一个Vue组件,它具有两个功能。 One attaches some data and populates various tags with that data, another gets the data from a json file and parses through it - applying it to the other function. 一个附加一些数据,并用该数据填充各种标签,另一个附加一个数据,从一个json文件获取数据并进行解析-将其应用于另一个函数。

(pseudo-code ahead!) (前面的伪代码!)

mounted: function() {
  function listBuilder(data) { some code }
  $.get("some-json"), function (data) {
    [some code...]
    listBuilder(data);
  }
}

On the page itself, this component exists twice. 在页面本身上,此组件存在两次。

<section>
  <my-component type="map"></my-component>
</section>
<aside>  
  <my-component></my-component>
</aside>

One component renders out the data in map form, while the other renders it out as a list. 一个组件以地图形式呈现数据,而另一个组件以列表形式呈现数据。 This all works just fine & dandy, except that on the map the data is rendered 2x - because, as you might have guessed, the component's logic is run twice - getting the json twice and processing it twice. 所有这些工作都很好,而且很花哨,只是在地图上将数据渲染2倍-因为您可能已经猜到了,该组件的逻辑运行了两次-两次获取json并对其进行了两次处理。

Is there a native "Vue" way to have (or designate) the code in the component to only run once on a page? 是否有一种本机的“ Vue”方式使(或指定)组件中的代码仅在页面上运行一次? I have a rather (in my opinion), hacky way of avoiding this right now, by applying a property to one of the components ("dumb"), that essentially skips some of the logic via a check if that property exists. 我有一个(我认为)相当灵活的方法,可以通过将属性应用于组件之一(“哑”)来避免这种情况,该方法实质上是通过检查该属性是否存在来跳过某些逻辑。 I feel there has got to be a more elegant and native way to handle this, however, but have come up dry in my google searches. 我觉得必须要有一种更优雅,更原生的方式来解决这个问题,但是在我的Google搜索中却干dry了。

Well I'm sure you'll get different responses, but I would have to agree with @pinoyyid and suggest extracting http part into a service for start, and then I would implement Vuex ( centralized local data that can be fairly easily integrated with Vue ) and then use Vuex actions and getters to get and process data. 好吧,我敢肯定您会得到不同的答复,但是我必须同意@pinoyyid并建议将http部分提取到服务中以进行启动,然后再实现Vuex(可以很容易地与Vue集成的集中式本地数据),然后使用Vuex操作和获取方法获取和处理数据。 This will completely separate your vue component from fetching and data processing. 这会将vue组件与获取和数据处理完全分开。 You will only fetch your data once and then use it as many times as you'd like. 您将只提取一次数据,然后根据需要使用多次。 And last but not least testing will be possible unlike now. 与现在不同,最后但并非最不重要的测试将是可能的。

One possible way is to have the component data loading moved to another parent element. 一种可能的方法是将组件数据加载移动到另一个父元素。 Other way of doing this is to have one global store (like vuex ) and then load the data out of it. 这样做的另一种方法是拥有一个全局存储(如vuex ),然后vuex加载数据。 This is just the type of problem vuex is solving. 这只是vuex正在解决的问题的类型。 If you don't want to use vuex just use the browser's Window.sessionStorage it is well supported and you can store all the data you need in it. 如果你不希望使用vuex只需使用浏览器的Window.sessionStorage这是很好的支持 ,你可以存储所有你在它需要的数据。 It is session based so you won't need to load the information every time. 它基于会话,因此您无需每次都加载信息。

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

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