1

I am using Laravel with Vue.js and Webpack / Laravel Mix. I have Single File Components which should make use of Mixins. The folder structure looks like this:

/app.js
/vue-components/Component.vue
/mixins/api/common.js

common.js defines a mixin like so:

export default {
   // all content goes here
}

And when I import it from app.js and console.log it, it shows the data:

import industries from "./mixins/api/common";
console.log(industries); // this shows the content
Vue.component(
    'some-component',
    require("./vue-components/Component.vue")
);

Within Component.vue I use mixins: [ industries ], and that gives me the following error:

Component.vue?bb93:73 Uncaught ReferenceError: industries is not defined

Question 1: Is it not possible to declare mixins globally and reference them from within a component?

To solve the issue I tried importing the mixin directly within the component instead of the global app.js file. But import industries from "./mixins/api/common"; within Component.vue throws the following error while trying to compile with npm run:

This relative module was not found:

* ./mixins/api/common in ./node_modules/babel-loader/lib?{"cacheDirectory":true,"presets":[["env",{"modules":false,"targets":{"browsers":["> 2%"],"uglify":true}}]],"plugins":["transform-object-rest-spread",["transform-runtime",{"polyfill":false,"helpers":false}],"babel-plugin-syntax-dynamic-import","webpack-async-module-name"],"env":{"development":{"compact":false}}}!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./resources/assets/js/vue-components/Component.vue

Question 2: If I have to import the mixin from within the Single File Components, what should the path look like?

1 Answer 1

2

As in Vue Document, You can declare mixin globally

//- build your mixin
const mixin = {
  created: function () {
    var myOption = this.$options.myOption
    if (myOption) {
      console.log(myOption)
    }
  }
}

Vue.mixin(mixin)


new Vue({
  myOption: 'hello!'
}) 
// "hello!"

You can also import mixin to use for each component.

In above your Component.vue, import path is not correct. You should do import industries from "../mixins/api/common"

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

1 Comment

thank you! My import path being incorrect was really stupid. Thank you.

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.