3

So I come from a React background and I was wondering how I could mimic this line: var modal = <Login/>

Here's what I have so far, but the output of the variable is a string instead:

<template src="./App.html"></template>

<script>
  import 'jquery'
  import 'uikit'
  import 'uikit/dist/css/uikit.min.css'

  import Login from '@/Login.vue'
  import Register from '@/Register.vue'

  export default {
    name: 'app',
    components: {
        "Login": Login,
        "Register": Register
    },
    data: function () {
        return {
            message: "first",
            modal: {
                body: "test",
                title: "body"
            }
        }
    },
    methods: {
        modal_open: function(type) {
            if (type === "login")
            {
                this.modal = {
                    body: Login,
                    title: "login"
                }
            }
            else
            {
                this.modal = {
                    body: Register,
                    title: "Register"
                }
            }
        },
    }
  }
</script>
3
  • What is the template? Commented Oct 12, 2017 at 3:52
  • 1
    @Bert Ah I found out how to do it. vuejs.org/v2/guide/components.html#Dynamic-Components I don't know why but I always seem to find the answer soon after I post the question. I'll answer it myself. Commented Oct 12, 2017 at 4:01
  • Thats what I wanted to know :) Glad you found it. Commented Oct 12, 2017 at 4:02

1 Answer 1

2

So in Vue it's apparently called Dynamic Components https://v2.vuejs.org/v2/guide/components.html#Dynamic-Components

From the given question, you should have <component :is="modal.body"></component> in your template. And rather than assigning an object, you assign the key (string) from the components variable inside the Vue instance. E.g. modal.body = "Login"

Sign up to request clarification or add additional context in comments.

2 Comments

is doesn't have to be a string. Also, an alternative to using a dynamic component would be using a render function.
@Bert Ah cool, could you update my answer (or make a new one, I'll mark it). You seem to be much more knowledgeable about this than me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.