2

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
8
  • 1
    How are you using all these ES6 implementations with transpiling to begin with? Commented Jan 15, 2018 at 19:11
  • 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. Commented Jan 15, 2018 at 19:15
  • 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. Commented Jan 15, 2018 at 19:17
  • @Alex that's what I don't want to use, cause I have django backend and it uses static files/folders for all these Commented Jan 15, 2018 at 19:17
  • 2
    @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. Commented Jan 15, 2018 at 19:29

2 Answers 2

3

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.
Sign up to request clarification or add additional context in comments.

2 Comments

Just to put my thoughts together ( from what I learnt through your answer and webpack's website ) , I should use webpack or browserify , cause they simplify things and in the end they compile these whole js files and ( not to forget ) components into a "bundle.js" file or something of that sorts . Am I correct ? It's done, cause moder webbrowsers still don't support js modules , am I right ?
@jame Yup. You'd need to add configuration though (which Vue templates already include). Then you can minify and uglify your code for production builds, maybe add vue-loader to enable single-file components, maybe pull in babel to enable cross-browser ES6 support... Either way, you'd have an easier time at separating your JS code from your Python templates.
3

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://v2.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.

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.