Vue.js

2019-04-17  本文已影响0人  沧海水i

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

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

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/>'
})

image
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('/');

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>

<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>

deleteCourse: function(courseId,index) {
            var _this = this;
            this.$http({
                method: 'delete',
                url: 'http://localhost:8080/api/course/' + courseId
            }).then(function() {
                alert('班课删除成功');
                 _this.courses.splice(index,1);
            });
        }

上一篇下一篇

猜你喜欢

热点阅读