Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js - set slot content programmatically

Tags:

vue.js

is there any way how can I set/override slot's content from inside the component? Like

Template:

<div>
    <slot></slot>
</div>

JS:

export default {
    ...
     mounted() {
        this.$slot.render("<button>OK</button>");
     }
    ...
}

I know I can use v-html on my element to dynamically push content into component template, but I mean not just pure HTML I mean HTML with Vue directives. Like:

JS:

export default {
    ...
     mounted() {
        this.$slot.default.render('<button @click="submit">OK</button>');
     },
    methods: {
        submit() {
            // Here I want to get :)
        }
    }
    ...
}

Basically I want Vue to render (like parse and render, not like innerHTML) certain string in scope of my component and put in on certain spot in my component. The reason is I get the new content from server via AJAX.

I'm sorry but I can't still get my head around after 2 days of googling.

Thanks a lot!

like image 553
n1_ Avatar asked Sep 10 '25 15:09

n1_


1 Answers

According to this you can instantiate a component programmatically and insert slot content as well.

import Button from 'Button.vue'
import Vue from 'vue'
var ComponentClass = Vue.extend(Button)
var instance = new ComponentClass({
    propsData: { type: 'primary' }
})
instance.$slots.default = [ 'Click me!' ]
instance.$mount() // pass nothing
this.$refs.container.appendChild(instance.$el)
like image 195
Marc Avatar answered Sep 13 '25 05:09

Marc