1

I have this method updateLocation calling location

setup(props, context) { 
  const updateLocation = (location = null) =>{
    if(location) {
      center.value = location
      context.emit('mapChange', context.location())
    }
  }
  const location = () =>  { return {
    center:center,
  }
}

but I get context.location is not a function. (In 'context.location()', 'context.location' is undefined)

I also tried without context

and also calling the method works from other functions; just not from emit apparently

2
  • How did you injected the location function into the context? Can you show us the plugin? Commented Sep 7, 2021 at 7:26
  • I did not "inject" anything; not sure what you mean. this is the only code related to this Commented Sep 7, 2021 at 7:27

3 Answers 3

1

You can try to rename location argument:

const updateLocation = (loc = null) =>{
if(loc) {
  center.value = loc
  context.emit('mapChange', location())
}

}

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

Comments

0

The context parameter has only 3 properties emit, attrs and slots, you could pass location by props like :

setup(props, context) { 
  const updateLocation = (location = null) =>{
    if(location) {
      center.value = location
      context.emit('mapChange', props.location())
    }
  }
}

1 Comment

I cannot access method from props ? I tried anyway; does not work
0

No need to use context if the function is within the same scope. Just give it a different name or it will shadow the variable location.

<script>
export default {
  setup(props, context) { 
    const getLocation = () => {
       return { center: center }
    }

    const updateLocation = (location = null) =>{
      if(location) {
        center.value = location
        context.emit('mapChange', getLocation())
      }
    }
  }
}
</script>

3 Comments

I am not sure what you mean ? the method is in the same file; why would I need to create a new file and import it ?
Well it wasn't clear it was in the same file... Then you don't have to use context, just run the function because it's in the same scope... I updated my answer.
Do you have any error with the code like this?

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.