3

I am just playing around with vuejs router and try to load a component. I used the sample code and changed foo

// Define some components
var Foo = Vue.extend({
    template: require('./components/test.vue')
});

var Bar = Vue.extend({
    template: '<p>This is bar!</p>'
});

// The router needs a root component to render.
// For demo purposes, we will just use an empty one
// because we are using the HTML as the app template.
var App = Vue.extend({})

// Create a router instance.
// You can pass in additional options here, but let's
// keep it simple for now.
var router = new VueRouter()

// Define some routes.
// Each route should map to a component. The "component" can
// either be an actual component constructor created via
// Vue.extend(), or just a component options object.
// We'll talk about nested routes later.
router.map({
    '/foo': {
        component: Foo
    },
    '/bar': {
        component: Bar
    }
})

// Now we can start the app!
// The router will create an instance of App and mount to
// the element matching the selector #app.
router.start(App, '#app')

I also tested it with

Vue.component('Foo', {
    template: require('./components/test.vue')
})

In my test.vue i have

<template>

<h2>Test</h2>

</template>

But not as soon as i use require i get everytime the error Required is not defined in my dev tools.

What do i wrong here?

2 Answers 2

2

require is a builtin in the NodeJS environment and used in Grunt build environments.

If you also want to use it in a browser environment you can integrate this version of it: http://requirejs.org

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

2 Comments

Yeah - thanks for your answer. I already figured it out because required and now i am using browserify.org
Node.js's require comes from its CommonJS module support. RequireJS is a library for AMD modules. They are not compatible. They both provide a function named require but it takes different arguments and expects modules to be designed differently.
1

(Author) This is outdated:

Use Browserify or Webpack as there is active support in the Vue community

http://vuejs.org/guide/application.html#Deploying_for_Production (dead link)

I personally used this repo of the Vue GitHub-org to get started quickly.

Edit:
This has moved on a bit in early 2018.

Deployment guide: https://v2.vuejs.org/v2/guide/deployment.html

'getting started' type repo: https://github.com/vuejs/vue-loader

2 Comments

All the links are dead
Thanks for the pointer. Adding the outdated label.

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.