美文网首页
Vue.js:工程化项目起步

Vue.js:工程化项目起步

作者: yu_liu | 来源:发表于2019-03-24 17:19 被阅读0次

    本例主要采用 vue-cli 配合 webpack 来创建项目,采用了 VueRouter ,引入 axios 库调用后端 API,渲染数据,实现了增删改查功能。

    创建新项目

    命令行进入某个目录

    D:\VueStudy

    创建项目

    vue init webpack vue-router-demo

    安装依赖

    进入 vue-router-demo 目录安,安装依赖:npm install

    修改配置文件

    修改 config 目录下 index.js 的 dev 端口为 80

    运行

    运行 npm run dev,打开 http://localhost,看到 Vue 主页 logo 即成功

    配置项目

    添加依赖

    package.json 的依赖文件,加入 axios 依赖

    "dependencies": {
        "vue": "^2.5.2",
        "vue-router": "^3.0.1",
        "axios": "^0.18.0"
    }
    

    在命令行输入 npm install,安装 axios 依赖

    引入依赖

    项目 src 目录下的 main.js 文件引入 axios

    import Vue from 'vue'
    import App from './App'
    import router from './router'
    import axios from 'axios'
    
    Vue.config.productionTip = false
    Vue.prototype.$http = axios
    
    /* eslint-disable no-new */
    new Vue({
      el: '#app',
      router,
      components: { App },
      template: '<App/>'
    })
    

    进行编码

    创建文件

    components 目录中创建一些 vue 文件

    配置路由

    router 下的 index.js 文件中配置路由

    import Vue from 'vue'
    import Router from 'vue-router'
    
    Vue.use(Router)
    
    export default new Router({
      // 去除#的hash模式
      mode: "history",
      routes: [
        {
          //我的班课
          path: '/',
          name: 'Index',
          component: resolve => require(['../components/Index.vue'], resolve)
        },
        {
          //任务中心
          path: '/task',
          name: 'Task',
          component: resolve => require(['../components/Task.vue'], resolve)
        },
        {
          //库管理
          path: '/lib',
          name: 'Lib',
          component: resolve => require(['../components/Lib.vue'], resolve)
        },
        {
          //个人中心
          path: '/ucenter',
          name: 'UCenter',
          component: resolve => require(['../components/UCenter.vue'], resolve)
        },
        {
          //新建班课
          path: '/new_course',
          name: 'NewCourse',
          component: resolve => require(['../components/NewCourse.vue'], resolve)
        },
        {
          //班课详情
          path: '/course/:id',
          name: 'CourseDetail',
          component: resolve => require(['../components/CourseDetail.vue'], resolve)
        }
      ]
    })
    

    路由跳转

    • 无参跳转
    <router-link to="/">
        <img src="./assets/logo.png" class="logo"/>
    </router-link>
    <router-link to="/task" class="nav-item">任务中心</router-link>
    
    • 带参跳转
    <router-link :to="'/course/' + course.courseId">
        <img :src="course.cover" />
    </router-link>
    
    • js 跳转
    _this.$router.push('/');
    

    RESTful 请求

    • GET 请求
    var _this = this;
        this.$http.get('http://localhost:8080/api/courses').then(function(response) {
            _this.courses = response.data;
    });
    
    <script>
    export default {
        name: 'CourseDetail',
        data() {
            return {
                id: this.$route.params.id,
                course: {}
            };
        },
        created() {
            var _this = this;
            this.$http.get('http://localhost:8080/api/course/' + this.id).then(function(response) {
                _this.course = response.data;
            });
        }
    };
    </script>
    
    • POST 请求
    <script>
    export default {
        name: 'NewCourse',
        data() {
            return {
                loginUserId: 1,
                course: {
                    courseName: '',
                    courseClass: '',
                    cover: ''
                }
            };
        },
        methods: {
            addCourse: function(course) {
                var _this = this;
                this.$http({
                    method: 'post',
                    url: 'http://localhost:8080/api/course',
                    data: {
                        userId: _this.loginUserId,
                        courseName: course.courseName,
                        courseClass: course.courseClass,
                        cover: course.cover,
                        finished: 0
                    }
                }).then(function() {
                    alert('新增班课成功');
                    _this.$router.push('/');
                });
            }
        }
    };
    </script>
    
    • DELETE 请求
    deleteCourse: function(courseId, index) {
        var _this = this;
        this.$http({
        method: "delete",
        url: "http://localhost:8080/api/course/" + this.id
        }).then(function() {
        alert("班课删除成功");
        _this.$router.push("/");
        _this.courses.splice(index, 1);
        });
    }
    
    • PUT 请求
    updateCourse: function(course) {
        var _this = this;
        this.$http({
        method: "put",
        url: "http://localhost:8080/api/course",
        data: {
            courseId: course.courseId,
            courseName: course.courseName,
            userId: this.id,
            courseClass: course.courseClass,
            cover: course.cover,
            courseCode: course.courseCode,
            finished: 1
        }
        }).then(function() {
        alert("班课结束");
        _this.$router.push("/");
        });
    }
    

    相关文章

      网友评论

          本文标题:Vue.js:工程化项目起步

          本文链接:https://www.haomeiwen.com/subject/bgwuvqtx.html