使用Broswerify和Vueify最近升級到Vue-2。在主應用程序中,我不得不要求vue/dist/vue.js
而不是'vue',因爲我沒有使用Webpack,但是現在當我使用Vue.use(require('vue-resource'));
時,我得到$ http是未定義的。 Vue1這工作順利。我錯過了什麼與Vue-2合作?Vue 2和Vue-Resource [無法讀取undefined(...)]的屬性'get'
錯誤:
Uncaught TypeError: Cannot read property 'get' of undefined(…)
Main.js:
require('./bootstrap');
var Vue = require('vue/dist/vue.js');
Vue.use(require('vue-resource'));
// var amazon = require('amazon-affiliate-api');
/**
* Next, we will create a fresh Vue application instance and attach it to
* the page. Then, you may begin adding components to this application
* or customize the JavaScript scaffolding to fit your unique needs.
*/
new Vue({
el: '#wrapper',
mounted: function() {
//use mounted instead of ready.
this.getHttp('/test', this.successFetch);
},
data: {
},
methods: {
successFetch: function (results) {
console.log(results);
},
//vue resource methods
getHttp: function (url,callback) {
const params = {
headers: {
'X-CSRF-TOKEN': this.token
}
}
this.$http.get(url, params).then(callback).catch(err => console.error(err));
},
Gulp.js:
const elixir = require('laravel-elixir');
require('laravel-elixir-vue-2');
require('browserify');
require('vueify');
elixir(function(mix){
mix.sass('main.scss')
.browserify('app.js');
});
的package.json:
{
"private": true,
"scripts": {
"prod": "gulp --production",
"dev": "gulp watch"
},
"devDependencies": {
"aliasify": "^2.1.0",
"bootstrap-sass": "^3.3.7",
"gulp": "^3.9.1",
"jquery": "^3.1.0",
"laravel-elixir": "^6.0.0-14",
"laravel-elixir-vue-2": "^0.2.0",
"laravel-elixir-webpack-official": "^1.0.2",
"lodash": "^4.16.2",
"vue": "^2.0.1",
"vue-resource": "^1.0.3",
"vueify": "^9.3.0"
},
"dependencies": {
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.16.0",
"browserify": "^13.1.1"
}
}
感謝VueCli幫了大忙。我仍然有一些簡單的瀏覽器問題。最後,我只使用他們擁有的browswerify版本。 –
是的,我使用它很多,因爲它確實給你一個非常穩固的起點 - 祝你好運! – GuyC