最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Define and reuse inline component in Vue3 - Stack Overflow

programmeradmin2浏览0评论

Is it possible to define a "sub ponent" inline in Vue3 which can be reused in the same .vue file/ponent, but without actually defining a new .vue file (i.e. without defining this sub ponent as a new ponent).

The use case is that I have a very specific way of formatting <select> options which is used for multiple <select>s within the same ponent (.vue file), but which will not be used anywhere else (it's also small, so I am inclined to define this options formatting part inline). I don't necessarily want to copy and paste the formatting (and it would be good to keep it within the same .vue file because it's small).

I realise that this is only syntactic sugar which may or may not be relevant in specific cases (I'm also not seeking advice on whether or not this is a good idea). I'm just looking for a way this can be done (if not, that's also an answer ;-))

Is it possible to define a "sub ponent" inline in Vue3 which can be reused in the same .vue file/ponent, but without actually defining a new .vue file (i.e. without defining this sub ponent as a new ponent).

The use case is that I have a very specific way of formatting <select> options which is used for multiple <select>s within the same ponent (.vue file), but which will not be used anywhere else (it's also small, so I am inclined to define this options formatting part inline). I don't necessarily want to copy and paste the formatting (and it would be good to keep it within the same .vue file because it's small).

I realise that this is only syntactic sugar which may or may not be relevant in specific cases (I'm also not seeking advice on whether or not this is a good idea). I'm just looking for a way this can be done (if not, that's also an answer ;-))

Share asked Apr 20, 2022 at 17:25 orangeorange 8,12215 gold badges87 silver badges154 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

You can define h() function to create vnodes inside your setup script in vue 3.

for example:

<template>
<MyComponent />
</template>
<script setup>
const MyComponent = h('div', { class: 'bar', innerHTML: 'hello' })
</script>

vue document will help you pletely. document

You could do something like this in Vue2

Vue.ponent('my-checkbox', {
  template: '<div class="checkbox-wrapper" @click="check"><div :class="{ checkbox: true, checked: checked }"></div><div class="title">{{ title }}</div></div>',
  data() {
    return { checked: false, title: 'Check me' }
  },
  methods: {
    check() { this.checked = !this.checked; }
  }
});

Probably still works

Vue3: Use defineComponent

Vue3 provides defineComponent which is probably what you want to consider for inline ponents. The docs for this could be deeper; I would seek out more tutorials and discussions on defineComponent.

You probably also want to understand rendering with h(). There are so many ways to use it! https://vuejs/guide/extras/render-function

Alternative: Regarding the render function, Vue3 has something called Functional Component which may be better for very simple inline ponents: https://vuejs/guide/extras/render-function#functional-ponents

In any case, here's one working implementation of defineComponent (code is Composition API + Typescript).

Example

<script setup lang="ts">

//...

const myInlineComponent = defineComponent(
    (props) => {
        // Use Composition API here, like in <script setup>
        // e.g. const count = ref(0)  

        // Must return something, which ultimately is the vnode output
        return () => {
            // Use h() render function...
            //e.g. return h('div', { class: `myClass` }, () => 'Some String')
            // or JSX...
            //e.g. return <div>{count.value}</div>

            // Example using render function h():
            return h(
                Button,
                {
                    onClick: (e:any) => {
                        console.log('Button clicked: ', props.someProp)
                    },
                    class: {
                        "myClass": true
                    }
                },
                () => 'Some String'
            )
        }
    },

    {
        props: {
            someProp: { type: string, required: true }
        }
    }

)

//...
</script>

Then use in a template

<myInlineComponent :someProp="'someString'" />
发布评论

评论列表(0)

  1. 暂无评论