简体   繁体   English

有没有办法初始化保留初始HTML的Vue对象

[英]Is there a way to initialize a Vue object preserving initial HTML

I want to use Vue.js to more easily manipulate the DOM but when I initialize a Vue object it rewrites initial data that is generated with backend at first before manipulating. 我想使用Vue.js来更轻松地操作DOM,但是当我初始化Vue对象时,它将重写在操作之前首先由后端生成的初始数据。

For example I have this markup: 例如,我有这个标记:

<ul id="list">
  <li v-repeat="elements" v-text="content">a</li>
  <li v-repeat="elements" v-text="content">b</li>
  <li v-repeat="elements" v-text="content">c</li>
</ul>

And then I want to use new Vue({ el: '#list' }) so that it would somehow read already existing markup and preserve it before manipulating via editing $data . 然后,我想使用new Vue({ el: '#list' })以便在编辑$data进行操作之前,它将以某种方式读取已经存在的标记并保留它。 Is this somehow achievable? 这可以实现吗?

I think this is not possible the way you want to do it. 我认为这不是您想要的方式。 Vue.JS doesn't operate on the DOM directly. Vue.JS不能直接在DOM上运行。 It reads the DOM elements, converts them into a render function and replaces the whole Element with the result of the render-function. 它读取DOM元素,将其转换为渲染函数,然后将整个Element替换为render-function的结果。

You should probably use JQuery to scan your DOM and fill your $data object (and possibly your template-string for Vue) and then initialie your Vue instance with this generated data. 您可能应该使用JQuery扫描DOM并填充$ data对象(以及可能的Vue模板字符串),然后使用此生成的数据初始化Vue实例。

But overall - you should rethink your application logic, because you seem to be doing something very convoluted, which could possibly be solved a lot easier. 但总的来说-您应该重新考虑应用程序逻辑,因为您似乎在做一些非常复杂的事情,而这可能很容易解决。 Whatever generates your DOM-Template could probably also directly render into a JS-variable, or even be accessed with an AJAX call... 无论生成您的DOM模板的任何内容,都可能直接渲染到JS变量中,甚至可以通过AJAX调用进行访问...

If you want to render a fallback approach if the client does not support JS or the CDN for Vue is not available you can use the script-template approach. 如果要在客户端不支持JS或CDN for Vue不可用时提供后备方法,则可以使用脚本模板方法。 Define your Vue.JS content in a script-tag, which will replace the original DOM, when Vue is ready. 在脚本标记中定义您的Vue.JS内容,当Vue准备就绪时,它将替换原始DOM。

Example: 例:

 function loadVue() { new Vue({ data: { values: [ 'aaa','bbb','ccc' ] }, template: '#list-template', el: '#list' }) } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.4/vue.js"></script> <ul id="list"> <li>a</li> <li>b</li> <li>c</li> <button onclick="loadVue()">Load Vue.JS</button> </ul> <script type="text/x-template" id="list-template"> <ul id="list"> <li v-for="v in values">{{v}}</li> </ul> </script> 

There's no reason you can't use a combination of already existing elements and a v-repeat like this 没有理由您不能使用已经存在的元素和像这样的v-repeat的组合

 new Vue({ el: '#list', data: { elements: [{ content: "d (v-repeat)" }, { content: "e (v-repeat)" }, { content: "f (v-repeat)" }] } }); 
 <script src="http://cdnjs.cloudflare.com/ajax/libs/vue/0.11.4/vue.min.js"></script> <ul id="list"> <li>a</li> <li>b</li> <li>c</li> <li v-repeat="elements" v-text="content">c</li> </ul> 
You just don't put a v-repeat on the elements that are already present. 您只是不对已存在的元素添加v-repeat

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

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