4

I created a hello.vue file, now how to use this in a html file? I already setup a webpack,

<template>
<p>Hello</p>

<p>{{message}}</p>

</template>

<script>
module.exports = {
    data: {
        message: 'hello'
    }
}
</script>

<style>
p {
    font-size: 14px
}
</style>
1
  • When you say, use the component in an HTML file, do you mean use it in a new Vue() as a component? Commented Mar 11, 2017 at 18:40

2 Answers 2

2

How do you want to use it?

Taking demonstation from official example: vue-hackernews-2.0. This is a component, so you import the component in another vue file like this:

import Comment from '../components/Comment.vue'

and you add in the list of components in that vue instance:

components: { Spinner, Comment },

Than you can use it in HTML like this:

<comment v-for="id in item.kids" :key="id" :id="id"></comment>
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, so we import the component into another .vue file, then use this .vue file in a html, is it possible to use the component directly in a html file?
@AngeloC A vue component will work inside a vue instance only, that is somewhere you will define new Vue({...})
2

You just have to include your bundled .js file in your HTML file and include mounter div - so Vue could know where to execute the app.

Your index file could looks something like this (showing only body section)

...
<body>

  <div id="app"></div>

  <script src="bundle.js">
</body>
...

Your main Vue file could looks like this:

import Vue from 'vue'
import Hello from 'hello.vue'

const app = new Vue({
  render: (h) => h(Hello)
}).$mount('#app')

And in this way you can store all other components into the hello.vue.

This is probably way that you should use when you are building SPA.

1 Comment

do you have to run vue-cli?

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.