day05 Vue.js工程化項(xiàng)目起步

使用vue-ci配合wekpack創(chuàng)建目錄

  • 使用命令行進(jìn)入某個磁盤目錄如E:\VueStudy
  • 輸入vue init webpack vue-router-demo創(chuàng)建目錄
  • 進(jìn)入vue-router-demo目錄輸入npm install安裝依賴
  • 修改config目錄下index.js的dev端口為80
  • 輸入npm run dev運(yùn)行項(xiàng)目,打開http://localhost,看到Vue主頁logo即成功
  • ctrl+c退出批處理狀態(tài)

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

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

  • 在命令行輸入npm install安裝axios依賴(每次添加依賴都需輸入命令,否則依賴無效)
  • 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目錄中創(chuàng)建一些vue組件,如圖
image
  • 配置路由,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)
        },
        {
            //任務(wù)中心
            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)
        }
    ]
})

  • 路由跳轉(zhuǎn)例子:

無參跳轉(zhuǎn)

<router-link to="/">
    <img src="./assets/logo.png" class="logo"/>
</router-link>
<router-link to="/task" class="nav-item">任務(wù)中心</router-link>

路徑傳參跳轉(zhuǎn):

<router-link :to="'/course/' + course.courseId">
    <img :src="course.cover" />
</router-link>

js跳轉(zhuǎn):

_this.$router.push('/');

  • 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/' + courseId
            }).then(function() {
                alert('班課刪除成功');
                 _this.courses.splice(index,1);
            });
        }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ## 框架和庫的區(qū)別?> 框架(framework):一套完整的軟件設(shè)計(jì)架構(gòu)和**解決方案**。> > 庫(lib...
    Rui_bdad閱讀 3,158評論 1 4
  • 本例主要采用vue-cli配合webpack來創(chuàng)建項(xiàng)目,采用了VueRouter,引入axios庫調(diào)用后端API,...
    陶然然_niit閱讀 846評論 0 6
  • 33、JS中的本地存儲 把一些信息存儲在當(dāng)前瀏覽器指定域下的某一個地方(存儲到物理硬盤中)1、不能跨瀏覽器傳輸:在...
    萌妹撒閱讀 2,258評論 0 2
  • 本例主要采用 vue-cli 配合 webpack 來創(chuàng)建項(xiàng)目,采用了 VueRouter ,引入 axios 庫...
    yu_liu閱讀 869評論 0 0
  • element-ui 文檔 Vue項(xiàng)目接口文檔地址 博客 session 和 cookie等 學(xué)什么? 1 如何使...
    cj_jax閱讀 4,079評論 0 10

友情鏈接更多精彩內(nèi)容