0

I want to use this javascript function to call on every template / layout / page in Nuxt.js (vuejs)

function setBoolean(val) {
  if (val) {
    return 1
  }
  return 0
}

on template I want to call it like

methods: {
  getValue() {
    this.value = setBoolean(false)
  }
}
1
  • You should then use middleware on the default layout of Nuxt. Commented Apr 1, 2021 at 7:21

2 Answers 2

1

You can use a global middleware for your Nuxt project.

// nuxt.config.js
export default {
  router: {
    middleware: ['custom-function']
  }
}

Then, write your function in /middleware/custom-function.js (at the root of your project). Like this, it will run globally.

More details available here: https://nuxtjs.org/docs/2.x/directory-structure/middleware/

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

2 Comments

I understand your idea but middleware functions run before rendering either a page or a group of pages (layout). Can you help me with this?
Not sure to get the issue here, isn't it what you want ?
0

You need create a plugin and inject to project.

Look at the documentation: https://nuxtjs.org/docs/2.x/directory-structure/plugins

or

you can use Vue filter.

1 Comment

This could be better as a comment rather an answer

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.