0

I want to use a vue component called vue-timeago:

import VueTimeago from 'vue-timeago'

Vue.use(VueTimeago, {
  name: 'Timeago', // Component name, `Timeago` by default
  locale: undefined, // Default locale
  locales: {
    'zh-CN': require('date-fns/locale/zh_cn'),
    'ja': require('date-fns/locale/ja'),
  }
})

However, I didn't built my webapp as a complete Vue app. I used Django's templates function and only added Vue as an add-on on each page:

<script>
    let app = new Vue({
        delimiters: ['[[',']]'],
        el: '#app',
        data: {
           
        },
        ...

How can I use that component mentioned above? Thanks a lot!

1 Answer 1

2

In theory you may add an attribute type="module" to the script tag and then import your component from a file but in this case you should handle all the dependencies manually. Obviously it's hard to do and it would be more practical to use a bundler to automate this part of the work.

I would propose the following steps:

  1. Create a source.js that contains:
import Vue from 'vue'
import VueTimeago from 'vue-timeago'

let app = new Vue({
        delimiters: ['[[',']]'],
        el: '#app',
        components: {
           'Timeago': VueTimeago
        },
        data: {
           
        },
        ...
  1. Compile source.js into a bundle.js using any bundler of your choice.
  2. link bundle.js file on your page: <script src="bundle.js"></script>
Sign up to request clarification or add additional context in comments.

Comments

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.