首页 \ 问答 \ 如何封装/包装VueJS组件?(How to encapsulate / wrap a VueJS component?)

如何封装/包装VueJS组件?(How to encapsulate / wrap a VueJS component?)

大家好,请原谅我的英语:-)

我有一个可以带动态插槽的Vue组件(插槽的名称将取决于道具)。

我在几个地方使用它,并且一些插槽总是存在。

为了避免冗余,我正在寻找一种方法来创建一个“包装”最终组件的组件,以允许我只定义添加插槽。

如果有一种“明显”的方式来实现它,我可能会错过它:-)

代码示例

没有“包装组件”

<b-table
  show-empty
  small
  hover

  [...some others and always present props...]

  :items="aDataVarThatWillChangeBasedOnTheContext"

  [...some others and uniq props...]
>
  <template slot="same-1">
   A slot that will always be present with the same content (for example, a checkbox in the first column)
  </template>

  <template slot="same-2">
   A slot that will always be present with the same content (for example, some action buttons in the last column)
  </template>

  [...some others and always present slots...]

  <template slot="not-the-same">
   A slot that is only used in this context (for example, a duration based on a row timestamp and a timestamp picked by the user)
  </template>

  [...some others and uniq slots...]
</b-table>

使用“换行组件”

<my-b-table
  :items="aDataVarThatWillChangeBasedOnTheContext"
>
  <template slot="not-the-same">
   A slot that is only used in this context (for example, a duration based on a row timestamp and a timestamp picked by the user)
  </template>
</my-b-table>

注意:动态插槽名称不可预测。 如果我突然需要一个“foo”列,我应该能够通过一个“foo”插槽(和一个“HEAD_foo”插槽,就我而言)

一些研究

在这里读到:

它们(功能组件)作为包装组件也非常有用。 例如,当你需要:

  • 以编程方式选择要委派的其他组件之一
  • 在将它们传递给儿童组件之前,操作儿童,道具或数据

而且“在将孩子,道具或数据传递给儿童组件之前操作儿童,道具或数据”似乎正是我所需要的。

我着眼于渲染功能,但很多东西似乎没有实现,比如v模型,我很难弄清楚如何通过动态插槽。

预先感谢您的回答(s)!

up:在07.03.2018我还没有任何关于如何解决这个案件的想法


Hi everybody, please pardon my english :-)

I have a Vue component that can take dynamic slots (the names of the slots will depend on a props).

I use it on several places and some of the slots are always present.

To avoid redundancy, I'm looking for a way to create a component that "wrap" the final component to allow me to define only the additionals slots.

If there is an "obvious" way to achieve it, I may have missed it :-)

Code example

Without a "wrap component"

<b-table
  show-empty
  small
  hover

  [...some others and always present props...]

  :items="aDataVarThatWillChangeBasedOnTheContext"

  [...some others and uniq props...]
>
  <template slot="same-1">
   A slot that will always be present with the same content (for example, a checkbox in the first column)
  </template>

  <template slot="same-2">
   A slot that will always be present with the same content (for example, some action buttons in the last column)
  </template>

  [...some others and always present slots...]

  <template slot="not-the-same">
   A slot that is only used in this context (for example, a duration based on a row timestamp and a timestamp picked by the user)
  </template>

  [...some others and uniq slots...]
</b-table>

With a "wrap component"

<my-b-table
  :items="aDataVarThatWillChangeBasedOnTheContext"
>
  <template slot="not-the-same">
   A slot that is only used in this context (for example, a duration based on a row timestamp and a timestamp picked by the user)
  </template>
</my-b-table>

Note: The dynamic slot name is not predictible. If I suddenly need a "foo" column, I should be able to pass a "foo" slot (and a "HEAD_foo" slot, in my case)

Some researches

I read here that:

They’re (the functionnal components) also very useful as wrapper components. For example, when you need to:

  • Programmatically choose one of several other components to delegate to
  • Manipulate children, props, or data before passing them on to a child component

And "Manipulate children, props, or data before passing them on to a child component" seems to be exactly what I need.

I looked on render function but a lot of things seems to be not implemented, like the v-model, and I have difficulties to figure out how to pass dynamic slots...

Thank you in advance for your(s) answer(s) !

up: At the 07.03.2018 I still dont have any idea about how to solve this case


原文:https://stackoverflow.com/questions/48807290
更新时间:2022-09-16 18:09

最满意答案

相关问答

更多
  • 您需要替换自定义用户模型 。 You will need to substitute custom user model.
  • 我会将所有内容保存在一起,并有一个列来跟踪登录是否成功。 请确保您不要将密码保存为纯文本。 I would save all in one, and have a column that keeps track of if the login was successful or not. Just make sure you dont save the passwords as plain text.
  • 如果你不想使用get_class,你会做这样的事情 class Client extends Account { protected $discr = 'client'; ... } class Administrator extends Account { protected $discr = 'administrator'; ... } 对象中的字段(不在数据库中) 并在Acount类中创建getType()方法 if you do not want to use ...
  • MySQL cli将位置参数视为数据库名称,使用--execute选项传递要运行的语句: mysql --login-path=remote --database=marketing --execute 'truncate table my_test_table' MySQL cli treats positional argument as database name, pass statement you want to run with --execute option: mysql --login ...
  • 一个好的做法是将ROLE表映射到USER表,这样用户可以拥有多个角色。 对于我在评论中提到的第二个问题,我会将所有数据放在user表中,但如果需要在两个表中分隔数据,则可以这样做。 只需将user_details表映射到user表即可。 ( user_details user_id ) 表用户 ID | USERNAME | PASSWORD ------------------------- 1 | bill | 09hk4352 /* password should be encoded ...
  • 在将输入密码保存到数据库之前对其进行加密 $user->password = bcrypt('MyNewPassword'); $user->save() (如果你没有修改Laravel使用另一个地穴算法,它会工作) bcrypt the input password before saving it to the database $user->password = bcrypt('MyNewPassword'); $user->save() (It will work if you didn't m ...
  • 看起来你正在混合一些东西。为了创建一个代理表,你不需要sp_remotelogin创建的“远程登录”。 但是,您需要外部登录,它应该在远程服务器中实际登录。 有关更多建议,您应该发布所采取的确切步骤以及错误输出。 Dears, kindly note that the problem is solved, the case was the password, the passwords for local login and remote login are different, so I add the ...
  • 选项3是你最好的,恕我直言。 1)安全 必须窃取多个表才能丢失所有客户端安全数据。 考虑限制您的风险和责任。 1a)还...... 你有什么需要知道哪个facebook登录与哪个openID登录相关联? 或许,但无论如何,对于数据窃贼来说,如果你把它们全部放在一起对它们来说肯定会很方便(选项1) 2)表格功能 - 例如触发器,计算字段,或者您可能(?)以独特的方式为每个提供商使用的内容。 3)如果你真的必须将一切都放在一个表中以用于奇怪/临时目的,你仍然可以用视图完成这个。 4)设计优化。 机会是,数据非常 ...

相关文章

更多

最新问答

更多
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)
  • 是否可以嵌套hazelcast IMaps?(Is it possible to nest hazelcast IMaps? And whick side effects can I expect? Is it a good Idea anyway?)
  • UIViewAnimationOptionRepeat在两个动画之间暂停(UIViewAnimationOptionRepeat pausing in between two animations)
  • 在x-kendo-template中使用Razor查询(Using Razor query within x-kendo-template)
  • 在BeautifulSoup中替换文本而不转义(Replace text without escaping in BeautifulSoup)
  • 如何在存根或模拟不存在的方法时配置Rspec以引发错误?(How can I configure Rspec to raise error when stubbing or mocking non-existing methods?)
  • asp用javascript(asp with javascript)
  • “%()s”在sql查询中的含义是什么?(What does “%()s” means in sql query?)
  • 如何为其编辑的内容提供自定义UITableViewCell上下文?(How to give a custom UITableViewCell context of what it is editing?)
  • c ++十进制到二进制,然后使用操作,然后回到十进制(c++ Decimal to binary, then use operation, then back to decimal)
  • 以编程方式创建视频?(Create videos programmatically?)
  • 无法在BeautifulSoup中正确解析数据(Unable to parse data correctly in BeautifulSoup)
  • webform和mvc的区别 知乎
  • 如何使用wadl2java生成REST服务模板,其中POST / PUT方法具有参数?(How do you generate REST service template with wadl2java where POST/PUT methods have parameters?)
  • 我无法理解我的travis构建有什么问题(I am having trouble understanding what is wrong with my travis build)
  • iOS9 Scope Bar出现在Search Bar后面或旁边(iOS9 Scope Bar appears either behind or beside Search Bar)
  • 为什么开机慢上面还显示;Inetrnet,Explorer
  • 有关调用远程WCF服务的超时问题(Timeout Question about Invoking a Remote WCF Service)