简体   繁体   English

.vue 文件中的 Vue.js 渲染函数

[英]Vue.js render function in .vue file

I'm new with vue.js so forgive me if what I write does not make sense to you.我是 vue.js 的新手,如果我写的内容对您没有意义,请原谅我。 It's not totally clear to me how to use render function inside a .vue file component.我并不完全清楚如何在 .vue 文件组件中使用渲染功能。

I got a component in a .vue file like this:我在 .vue 文件中有一个组件,如下所示:

<template>
    <transition name="slide-fade">
        <div class="drop-list" v-html="items">
        </div>
    </transition>
</template>

<style>
</style>

<script>
    export default{
        name: "drop-item",
        props:['items'],
        data(){
            return{}
        },
        render(createElement) {
            // create the list
        }
    }
</script>

Basically I have 3 component that alternately sends content ("items") to this one, mi goal is to render an unordered list inside it with a "@click='doSomenthing'" directive inside every list-element and "doSomething" depends on which is the component that sent the items to this one.基本上我有 3 个组件交替发送内容(“项目”)到这个组件,我的目标是在其中呈现一个无序列表,每个列表元素中都有一个“@click='doSomenthing'”指令,而“doSomething”取决于这是将项目发送到此的组件。 Any help will be appreciated任何帮助将不胜感激

Firstly, you do not put render functions inside components, you simply pass the data as a prop .首先,您不要将render functions放在组件中,您只需将数据作为prop传递。 If you need to know which component passed the list of items, then you can simply pass a prop to let your component know what action to take, here's a basic example:如果您需要知道哪个组件传递了项目列表,那么您可以简单地传递一个 prop 让您的组件知道要执行什么操作,这是一个基本示例:

<template id="my-list">
  <div>
    <ul>
      <li v-for="item in items"><a href="#" @click="doSomething">{{item}}</a></li>
    </ul>
  </div>
</template>

<script type="text/javascript">
export default {
  props: ['items', 'action'],
  methods: {
    doSomething() {
      switch (this.action) {
        case 1:
          console.log("I'm doing action 1");
          break;
        case 2:
          console.log("I'm doing action 2");
          break;
        default:
          console.log("I'm doing default action");
      }
    }
  }
}
</script>

You can then set the component up in our parent and pass an action, I'm just passing a number here:然后,您可以在我们的父级中设置组件并传递一个动作,我只是在这里传递一个数字:

<my-list :items="items" :action="2"></my-list>

Here's a JSFiddle: https://jsfiddle.net/uckgucds/这是一个 JSFiddle: https ://jsfiddle.net/uckgucds/

If you are writing complex actions then you may want to write separate components for each list type, rather than a switch statement, you can then use a mixin to create the duplicate sections.如果您正在编写复杂的操作,那么您可能希望为每个列表类型编写单独的组件,而不是switch语句,然后您可以使用mixin来创建重复的部分。

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

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