[英]How to specify a place to render a DOM element in Vue.js?
I am building a client in Vue.js for an API. 我正在Vue.js中为API建立客户端。 This API gives access to some objects that I am able to create, read, update and delete. 通过此API,可以访问一些我可以创建,读取,更新和删除的对象。
I created a Vue component that fetch the schema of the object I want to access through the API and then generates a form based on how the object is defined API-side. 我创建了一个Vue组件,该组件可获取要通过API访问的对象的架构,然后根据对象在API端的定义方式生成表单。
I also added the possibility to add custom fields to the generated form, in addition to the fields generated from the object schema. 除了从对象模式生成的字段之外,我还添加了将自定义字段添加到生成的表单的可能性。
Some of these custom additional fields are serializable (I send them back to the API) and some are not. 这些自定义附加字段中的某些可序列化(我将它们发送回API),而某些则不可。 A use case for this would be the password of a User
object. 一个用例是User
对象的密码。 There is the classic password
field but I am adding a confirm_password
field. 有经典的password
字段,但我要添加一个confirm_password
字段。 This confirm_password
field is not sent back to the API, it is only used to verify the user's input in the front-end. 该confirm_password
字段不会发送回API,仅用于验证用户在前端的输入。
Until now, all this is up and working on my front-end. 到目前为止,所有这些工作都已经在我的前端上进行了。 After this long introduction, here is my question : 经过漫长的介绍之后, 这是我的问题 :
Is it possible to tell vue to put a specific element just after another one ? 是否可以告诉vue将特定元素放在另一个元素之后? For example, let's says my component is organized as follows : 例如,假设我的组件的组织方式如下:
<template>
<div>
<div v-for="field in AutoGeneratedFields" >
//Do something to display the fields
</div>
<div v-for="field in SerializableCustomFields" >
//Do something to display the fields
</div>
<div v-for="field in NonSerializableCustomFields" >
//Do something to display the fields
</div>
</div>
</template>
So, I have 3 for
loops. 因此,我有3个for
循环。 One to display the fields generated from the object schema, one for the custom fields I want to serialize and one for custom fields I don't want to serialize. 一种用于显示从对象模式生成的字段,一种用于我要序列化的定制字段,另一种用于我不想序列化的定制字段。
I would like my confirm_password
field to be displayed just after my password
field, but since they are not created in the same loop, It's very hard to guarantee they will be displayed one after another. 我希望将我的confirm_password
字段显示在我的password
字段之后,但是由于它们不是在同一循环中创建的,因此很难保证将它们依次显示。
TLDR: Is it possible, and if yes how, to tell Vue to put my confirm_password
field just after my password
field even though the confirm_password
field might be generated later and potentially after there are already other fields generated between password
and confirm_password
? TLDR:是否可以告诉Vue是否将我的confirm_password
字段放在我的password
字段之后,如果可以的话,也可以告诉我,即使在以后可能会生成confirm_password
字段,并且可能在password
和confirm_password
之间已经生成其他字段之后?
NB : I am using quasar framework, but I didn't find anything there to move element around either. 注意 :我使用的是类星体框架,但是我没有找到任何可以移动元素的东西。
I think using jquery would be a better option in this case. 我认为在这种情况下,使用jquery将是更好的选择。 After rendering the form elements, you can use jquery to grab the password field and add comfirm password field after password field using after() function of jquery. 呈现表单元素后,可以使用jquery来获取密码字段,并使用jquery的after()函数在密码字段之后添加comfirm密码字段。
If you want to do it Vue-way it's possible. 如果您想这样做,可以。
An example is here: http://jsfiddle.net/eywraw8t/335439/ 此处是一个示例: http : //jsfiddle.net/eywraw8t/335439/
Some additional methods
and a computed
property are required. 需要一些其他methods
和computed
属性。 But what if you need it to automatically fit without hardcoding? 但是,如果您需要它来自动适应而不需要硬编码怎么办?
Here is an example that does that: http://jsfiddle.net/eywraw8t/335504/ 这是一个执行此操作的示例: http : //jsfiddle.net/eywraw8t/335504/
But it requires you to name your non-serializable custom fields in a similar way to your serializable fields, in this example: 但这要求您以与可序列化字段相似的方式命名不可序列化的自定义字段,在此示例中:
Serializable : password
, email
可序列化 : password
, email
Non-Serializable : password_confirm
, email_confirm
不可序列化 : password_confirm
, email_confirm
If there is an object in NonSerializableCustomFields
that contains password_
or email_
it is appended after it's Serializable
equivalent. 如果NonSerializableCustomFields
中存在一个包含password_
或email_
的对象, email_
其附加到等效的Serializable
之后。
If you have access to the server and can edit your endpoints controllers appropriately, you can also pass order
property which says in which order fields should be rendered. 如果您有权访问服务器并可以适当地编辑端点控制器,则还可以传递order
属性,该属性表示应在哪个订单字段中呈现。 You would then map all your fields arrays into one on fetched while adding type
property which says if the object is serializable
. 然后,在添加type
属性(表示对象是否可serializable
时,将获取的所有字段数组映射为一个。 You would then filter serializable
objects before submitting form. 然后,您将在提交表单之前过滤可serializable
对象。 Example here: https://codesandbox.io/s/kxvq3rwwv 此处的示例: https : //codesandbox.io/s/kxvq3rwwv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.