Using Vue JS with Require JS?

喜欢而已 提交于 2019-12-23 13:32:51

问题


I installed the following via Bower:

  • jQuery 3.1.0
  • Vue 1.0.26
  • Require.js 2.2.0

But when I load my site, "Vue" is undefined.

I tried var vue = require('Vue') and stuff, but it doesn't seem to work.

Vue says it is a AMD module and so is Require.js... What am I missing?


回答1:


var vue = require('Vue') won't work by itself. Either you:

  1. Change it to the form of require that takes a callback:

    require(['Vue'], function (vue) {
      // code that uses Vue
    });
    
  2. Or put your require call in a define call:

    define(function (require) {
      var vue = require('Vue');
    });
    

As Linus Borg pointed out, require with a single string is CommonJS syntax, which RequireJS supports only if it appears in the callbacks passed to define. (See this.)



来源:https://stackoverflow.com/questions/38302260/using-vue-js-with-require-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!