0

I have a component that looks like this:

@Component({
    computed: {
        [this.stateModel]: {
            get() {
                return this.$store[this.stateModel];
            }
        }
    }
})
class Component extends Vue{
    @Prop({ default: '' }) private stateModel!: string;
}

Which I'm trying is bind stateModel as a property when I use this component. stateModel should be a field in the state and could be injected into the component. Typescript is throwing me an error that said:

Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.

I tried to make an Interface and set it as computed, but it didn't work.

Any help is highly appreciate it.

4
  • If stateModel is a prop, are you trying to update the value of stateModel with that from the store when stateModel prop changes? Commented Sep 29, 2020 at 5:17
  • Hi @Tony, what I'm trying to do is inject a name property stateModel of the state Commented Sep 29, 2020 at 14:55
  • Did the watcher work for you? Commented Sep 30, 2020 at 8:53
  • @Tony No, it didn't. stateModel is not a property, it should be dynamic. I mean, if I use <Component :stateModel="foo">, foo should be in the state. Commented Sep 30, 2020 at 15:35

1 Answer 1

1

I tried to use the this keyword in a computed property and I got two errors.

'this' cannot be referenced in a computed property name

'this' implicitly has type 'any' because it does not have a type annotation

Alternatively, you can use a watcher instead

import { Watch } from 'vue-property-decorator';

@Component
class Component extends Vue{
    ...

    @Watch('stateModel')
    stateModelHasChanged(newValue: number) {
      this.stateModel = this.$store[this.stateModel];
    }
    ...

}

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

Comments

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.