vue-resource vue-axios传递数据

使用vue-resource传递数据

在Home.vue组件中传送数据




src/model/Storage.js中封装的数据
//封装操作localstorage本地存储的方法   模块化的文件
// nodejs  基础
var storage={
    set(key,value){
        localStorage.setItem(key, JSON.stringify(value));
    },
    get(key){
        return JSON.parse(localStorage.getItem(key));
    },remove(key){
        localStorage.removeItem(key);
    }
}
export default storage;
将Hone.vue引入根节点



将vue-resource引入main.js
import Vue from 'vue';
import App from './App.vue';
/*使用vue-resource请求数据的步骤
1、需要安装vue-resource模块,  注意加上  --save
  npm install vue-resource --save /  cnpm install vue-resource --save  
2、main.js引入 vue-resource
    import VueResource from 'vue-resource';
3、main.js  Vue.use(VueResource);
4、在组件里面直接使用
    this.$http.get(地址).then(function(){
    })
*/
import VueResource from 'vue-resource';
Vue.use(VueResource);
new Vue({
  el: '#app',
  render: h => h(App)
})

Axios请求数据

src/components/Home.vue (组件里请求数据)



以下方法是引入的方法

user.js

var vue = new Vue({
    el: "#app",
    data: {
        user: {id:"",username:"aaa",password:"",age:"",sex:"",email:""},  //定义数据类型Json
        userList: []     //代表多个数据,与v-for配合使用
    },
    methods: {
        findAll: function () {
            var _this = this;    //需在外部定义对象代表vue对象
            axios.get("/vuejsDemo/user/findAll.do").then(function (response) {
                _this.userList = response.data;       //接收返回数据
                console.log(_this.userList);
            }).catch(function (err) {
                console.log(err);
            });
        },
        findById: function (userid) {
            var _this = this;  //必须将对象定义在外边,代表是vue的对象,给axios引用
            axios.get("/vuejsDemo/user/findById.do", {
                params: {             //定义传输数据
                    id: userid
                }
            }).then(function (response) {
              _this.user = response.data;
                $('#myModal').modal("show");        
            }).catch(function (err) {
            });

        },
        update: function (user) {
            var _this = this;
            axios.post("/vuejsDemo/user/update.do",_this.user).then(function (response) {
                _this.findAll();
            }).catch(function (err) {
            });
        }
    },
    created(){               //代表每次刷新加载的方法
        this.findAll();
    }
});

user.html


你可能感兴趣的:(vue-resource vue-axios传递数据)