2
votes

everyone.

I have a trivial doubt on making vue components.

I don't want to use browserify or webpack , cause I am working in django and it has most of it's templates in static files , although I read this , which does describe how to take in account both ( but that's for some other day ).

Problem :

I am making a single file component which I have to import and use, using my router but I can't, as the import just doesn't happen.

My Hello.vue

<template>
 Some HTML code here.
</template>
<script>
module.exports = {
  data() {
    return {
      coin : []
    }
  },
  beforeRouteEnter (to, from, next) {
    axios.get('my-django-rest-api-url')
    .then(response => {
      next(vm => {
        vm.data = response.data
      })
    })
  }
}
</script>

I have it in the index.html file itself , no other .js file,

<script>
    import Hello from '@/components/Hello.vue'
    Vue.use(VueRouter);
    const dashboard = {template:'<p>This is the base template</p>'};
    const profile = {
      template: '#profile_template',
      data () {
        return {
          profile_details: []
        }
      },
      beforeRouteEnter (to, from, next) {
        axios.get('my-api-url')
        .then(response => {
          next(vm => {
            vm.profile_details = response.data
          })
        })
      }
    }
    const router = new VueRouter({
    routes: [
          { path: '/', component: dashboard },
          { path: '/profile', component: profile },
          { path: '/hello', component: Hello }
        ]
    });
    new Vue({
      router : router,
    }).$mount('#app');
</script>

What all I've tried :

1.<script src="../components/Hello.js" type="module"></script> and removing the import statement as suggested here

  1. Replacing my Hello.js's code with this : export const Hello = { ...
  2. Making a Hello.js file and importing it like this import Hello from '../components/Hello.js';

Error :

  1. **Mozilla ( Quantum 57.0.4 64 bit ) ** : SyntaxError: import declarations may only appear at top level of a module
  2. **Chrome ( 63.0.3239.108 (Official Build) (64-bit) ) ** :Uncaught SyntaxError: Unexpected identifier
P.S. : I have tried these in various combinations
2
How are you using all these ES6 implementations with transpiling to begin with? - Ohgodwhy
Neither ES6 import/export, nor CommonJS module.exports can work in any browser. You need Webpack + Babel for the former and Browserify for the latter. - Alex
Plus, I don't see any single file components here, you just have regular components so far. If you did have them, you'd definitely need Webpack with the proper loader. - Alex
@Alex that's what I don't want to use, cause I have django backend and it uses static files/folders for all these - jame
@jame "compiling" is the work of webpack or browserify or tools like them. Your asking for opposite things. You cannot have a single file component and not use a webpack or the like. You'll have to register your components programatically and then include them using a script tag. - etchesketch

2 Answers

2
votes

Not a Vue.js guru, but here are a few perspectives that might help you.

  • Module loading is still not supported on modern browsers by default, and you'd need to set special flags in order to enable it (which the users of your app probably won't do).
  • If you insist on using import and export, you'd need Webpack. And most certainly Babel (or any other ES6 transpiler, e.g. Buble) as well.
  • If you prefer module.exports, then you'd need Browserify. It enables support for CommonJS in browser environments.
  • If neither is doable, then your best bet is defining Vue components in global scope. You can split them across separate files, and import each with a <script> individually. Definitely not the cleanest approach.
  • Single file components typically go inside of .vue files, but either way they require vue-loader which can be added and configured (again) with a bundler.
  • Last option is to just use an existing setup in place, if there is any (is there?). If you already have RequireJS, UMD, or something similar in place, adjust your components to fit that. Otherwise, use <script>s.
2
votes

You are trying to do something which is not possible. Vue Single file components are not supported as raw component file by web browsers. The single file component is supposed to be compiled.

Please see this for more: https://vuejs.org/v2/guide/single-file-components.html

In Webpack, each file can be transformed by a “loader” before being included in the bundle, and Vue offers the vue-loader plugin to translate single-file (.vue) components.

A vue single file component is first "translated" (compiled) to pure javascript code which is use-able by browsers.