vue access imported variable in template
Asked Answered
P

2

7

Is there some way to use an imported object in the template without specifying an alias/property for it in the vue component. I.e. can I somehow use variables not defined on "this"

 <template>
        <div v-if="Store.loaded"></div> //Store not defined on component
        <div v-if="store.loaded"></div> //works
    </template>

    <script>
        import Vue from 'vue'
        import { Component } from 'vue-property-decorator'
        import Store from "../store" // can I somehow use this directly?

        @Component
        export default class App extends Vue {
            store = Store // is there some way so I don't need this line?
        }
    </script>
Papaveraceous answered 17/12, 2017 at 21:38 Comment(0)
M
2

Can I somehow use variables not defined on "this"

This is not possible as far as I know.

However, looking at your example I can see that you're trying to access properties of the Vuex store. Vuex exposes the store on every instance via this.$store, so you should be able to do this in your templates:

<div v-if="$store.state.loaded">
Michal answered 17/12, 2017 at 23:22 Comment(1)
Unfortunately it is not a vuex store. It is my own Store object. But maybe I could write a plugin that attaches my store to vue like vuex doesPapaveraceous
O
1

You could use a plugin, like vuex does. But generally, it is a bad idea.
It makes other people who work with your code question the source of Store field.

Oecd answered 17/12, 2017 at 21:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.