# 相关配置版本:
laravel 5.1 LTS
laravel-elixir 3
vue 1.0.26
vue-router 2.0.0
vue-resources 1.0.3
Use browserify
that comes with laravel-elixir
to compile vue, vue-router, vue-resource
After successfully outputting the js file, an error is reported when using it: Uncaught TypeError: Cannot read property 'use' of undefined
The compiled js file is partially as follows:
var Vue = require("vue");
var VueRouter = require('vue-router');
var VueResource = require('vue-resource');
Vue.use(VueRouter);
Vue.use(VueResource);
gulpfile.js
// 局部
elixir(function(mix) {
mix.browserify('index.js', 'public/dist/js/index.js');
mix.browserify('new.js', 'public/dist/js/new.js');
});
Problem:
The two files compiled above depend on the same require
configuration. Index.js can be used, but new.js will report an error. Check each dependency separately through console.log()
. Vue
in new.js
is undefined
. I don’t know if it is possible. What's the reason, or how to find out where the problem is?
In addition, how to make vue
These dependencies only need to be loaded once, and do not need to be packaged together in the file. I configured browser-shim
, but it seems to have no effect. I don’t know why. I used it wrong or something:
package.json
"browserify": {
"transform": [
"vueify",
"browserify-shim",
"stringify"
]
},
"browserify-shim": {
"bootstrap-sass": {
"depends": [
"jquery:jQuery"
]
},
"vue": "global:Vue"
}
Finally found the cause of the problem (expression: bursting into tears)
index.js
里的Vue
可正常使用,而new.js
的Vue
则是undefined
,猜测难道它不可以多次require
,但是VueRouter, VueResource
也重复require
了,却是可用的,思索再三,想起可能是因为package.json
里写了一句把Vue
Set it as a global variable, so I removed the sentence, compiled it again, and found that it was successful (what a side effect of half-knowledge==). The removed setting is as follows:PS: Thanks to my friend
Vicent Ye
for helping me find the problem.