2

I have hopefully a very easy question: I'd like to link components from within components in VueJS.

What I have so far:

create.js (which will be rendered with webpack to create.min.js)

var Vue = require('vue')
var App = require('./components/App.vue')
var Language = require('./components/Language.vue')
var vm = new Vue(App,'#app')

components/App.vue

<template>
  <h1>Create!</h1>
  <ss-lang></ss-lang>
  <pre>
    {{ $data | json }}
  </pre>
</template>

<script>
    module.exports = {
        name : 'Create',
        components : {
        'ss-lang' : new Language()
        }
    }
</script>

components/Language.vue

<template>
  <select v-model="selectedLanguage">
    <option v-for="language in languages" v-bind:value="language.value">
      {{ language.name }}
    </option>
  </select>
</template>

<script>
module.exports = {
  name : 'Language',
  data : function(){
    return {
      selectedLanguage : '',
      languages : [
        {value: 'be_en', name: 'English'},
        {value: 'be_fr', name: 'Français'},
        {value: 'be_de', name: 'Deutsch'},
        {value: 'be_nl', name: 'Nederlands'},
        {value: 'be_es', name: 'Español'}
      ]
    }
  }
}
</script>

When I open in the browser, I get the message that "Uncaught TypeError: Language is not a function". I was hoping that by connecting 'ss-lang' to the Language would be sufficient in connecting (as I added Language in the create.js).

What am I doing wrong? (eg which part of the documentation am I missing?)

1 Answer 1

1

You should import the Language component inside App.vue. In your example it only was imported in the context of create.js :

//App.vue
<script>
    var Language = require('./relative-path-to-the-component/Language.vue')
    module.exports = {
        name : 'Create',
        components : {
        'ss-lang' : Language
        }
    }
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer, I actually tried this option before already and unfortunately this gives me the same error message: "Uncaught TypeError: Language is not a function"
Pass the Language object as is, don't instatiate it.
Ah thanks, I was still doing: new Language(). Now, no error anymore (also no select box, but that is an other story. Accepted 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.