0

I have latest Vue project from cli. Just playing around with vuex and the store.

Default project has this initialization.

import Vue from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';
import './registerServiceWorker';

Vue.config.productionTip = false;

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount('#app');

The store looks like this:

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
      stateName: 'STORE-NAME',

  },
  mutations: {

  },
  actions: {

  },
});

My SFC looks like this:

<template>
  <div class="about">
    <h1>This is the Probate page</h1>
    <p>State Name: {{ localName }}</p>
  </div>
</template>

<script lang="ts">


    export default {

        data(){
            return {
                localName: 'COMPONENT-NAME',
            };
        },

        created(){
                        this.localName = this.$store.state.stateName;

        },
    };

</script>

The error I'm getting is this when running 'npm run serve':

ERROR in /Users/*/Documents/projects/legalcove-single-page-app/src/views/Probate.vue
27:14 Property 'localName' does not exist on type '{ data(): { localName: string; }; created(): void; }'.
    26 | 
  > 27 |             this.localName = this.$store.state.stateName;
       |              ^
    28 | 
    29 |         },
    30 |     };
Version: typescript ERROR in /Users/*/Documents/projects/lc-single-page-app/src/views/Probate.vue3.1.1
, tslint 5.11.027:31
 Property '$store' does not exist on type '{ data(): { localName: string; }; created(): void; }'.
Time:   3011 ms
    26 | 
  > 27 |             this.localName = this.$store.state.stateName;
       |                               ^
    28 | 
    29 |         },

But while running the app, I don't get any errors in the browser and it works as I would expect it to. I get stateName value displayed on my screen.

Pompey Magnus
  • 2,191
  • 5
  • 27
  • 40
  • Did you mean to use `TypeScript` with ` – Psidom Oct 24 '18 at 00:29
  • yes, i'm using typescript – Pompey Magnus Oct 24 '18 at 00:37
  • SFC is would be slightly different for typescript. see the example in https://github.com/Microsoft/TypeScript-Vue-Starter#adding-a-component. you need to use `export default Vue.extend({` – Jacob Goh Oct 24 '18 at 03:24
  • You are correct, that worked, but more correct I should have been using decorators, which is explained further down in the reading. https://github.com/Microsoft/TypeScript-Vue-Starter#using-decorators-to-define-a-component – Pompey Magnus Oct 25 '18 at 01:38

0 Answers0