1

So I’m passing a component as a variable through the store e.g.

<template>
    <div>
        <component :is="store.state.General.body"></component>
    </div>
</template>

<script>
    import store from "@/store"
</script>

Now I was wondering how I would pass the component with props because this is how I’m doing it:

<script>
    import Input from "@/components/Input"

    methods: {
        example() {
            store.commit("general_set_modal", {body: Input, title: "New "+page})
    }
</script>

it’s being rendered properly, but just lacks the desired props.

5
  • Maybe dynamic props is what you're looking for? (Look for "pass all the properties") Commented Feb 14, 2018 at 23:31
  • @RoyJ Not quite, since I can't exactly use html tags inside the script area. I would probably have to assign it like var input = Input then have input assign the props like input.prop.example = "this is an example prop" Commented Feb 15, 2018 at 0:06
  • You would make the binding in the template area: <component :is="store..." v-bind="somePropsObjectYouBuilt"> Commented Feb 15, 2018 at 0:52
  • @RoyJ The purpose of that component is just to display the component passed in. It's a modal, so I want to be able to throw anything into there and separate the dependencies. Commented Feb 15, 2018 at 1:57
  • In your example above, what would be the prop(s) you want passed in? Commented Feb 15, 2018 at 2:01

1 Answer 1

1

I'd do something like

<template>
    <div>
        <component :is="store.state.General.body" v-bind="store.state.General.props"></component>
    </div>
</template>

<script>
    import store from "@/store"
</script>

and have the store item look like

<script>
    import Input from "@/components/Input"

    methods: {
        example() {
            store.commit("general_set_modal", {body: Input, props: {title: "New "+page}})
    }
</script>
Sign up to request clarification or add additional context in comments.

7 Comments

Ah, I must have misunderstood you before. yeah this works great.
Although I seem to be getting a warning [Vue warn]: Error in callback for watcher "function () { return this._data.$$state }": "Error: [vuex] Do not mutate vuex store state outside mutation handlers." even though I'm only calling it as a read.
I don't have any idea what that's about. There's no watcher in the code samples you included.
Yeah I don't have watchers implemented either, which is why it's weird. Well the one watch that I have is in a completely unrelated file :\ I am accessing the variable through the prop
Vuex maintains a deep watch in strict mode to catch inadvertent mutations. Maybe that's going on.
|

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.