vue入门记录 一

1.main.js配置

import Vue from 'vue'
import App from './App.vue'
import Router from "./router";
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import qs from 'qs'
import axios from 'axios' //引入axios
Vue.prototype.$alert = ElementUI.Alert
Vue.prototype.$ajax=axios
Vue.prototype.$qs=qs
Vue.use(ElementUI);
Vue.use(Router)
new Vue({
  router:Router,
  render: h => h(App),

}).$mount('#app')

2.vue3.0 本地跨域

新建vue.config.js丢在根目录。

module.exports = {
    devServer: {
        proxy: {
            '/api': {
                target: 'http://127.0.0.1:8008/', //对应自己的接口
                changeOrigin: true,
                ws: true,
                pathRewrite: {
                    '^/api': '/'
                }
            }
        }
    }
}

3.访问后端接口,解决传参为null,使用qs

<template>
    <el-form label-width="80px" :model="user"
             @submit.prevent="submit($event)">
        <el-form-item label="编号">
            <el-input v-model="user.id"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
            <el-input v-model="user.name"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
            <el-input v-model="user.age"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit">立即创建</el-button>
            <el-button>取消</el-button>
        </el-form-item>
    </el-form>

</template>

<script>
    export default {
        name: "ComponentB",
        data() {
            return {
                user:{
                    id:'',
                    name:'',
                    age:''
                }
            }
        },
        methods: {
            onSubmit() {
                console.log('submit!');
                console.log(this.user.age)
               var user = this.$qs.stringify({
                    id:this.user.id,
                    name:this.user.name,
                    age:this.user.age==''?0:this.user.age,
                });
                //vue-resource
                this.$ajax.post('/api/user/addUser', user).then(res => {
                    // success callback
                   console.log(res.data.body)
                this.$alert(res.data.body)
                }, err => {
                    alert(err)
                    // error callback
                });
            }
        },
    }

你可能感兴趣的:(Vue,vue,vue.js,node.js)