06_06.接口配置

2017-11-19  本文已影响0人  Robyn_Luo
1. 要操作的文件:
2. 代码:
<template>
    <nav class="mui-bar mui-bar-tab">
        <router-link class="mui-tab-item mui-active" to="/">
            <span class="mui-icon mui-icon-home"></span>
            <span class="mui-tab-label">首页</span>
        </router-link>
        <router-link class="mui-tab-item" to="/">
            <span class="mui-icon mui-icon-extra mui-icon-extra-gift"></span>
            <span class="mui-tab-label">商品购买</span>
        </router-link>
        <router-link class="mui-tab-item" to="/">
            <span class="mui-icon mui-icon-extra mui-icon-extra-cart"><span class="mui-badge">0</span></span>
            <span class="mui-tab-label">购物车</span>
        </router-link>
        <router-link class="mui-tab-item" to="/">
            <span class="mui-icon mui-icon-gear"></span>
            <span class="mui-tab-label">设置</span>
        </router-link>
    </nav>
</template>

<script>
    export default {
        
    }
</script>

<style scoped>

</style>
<template>
    <header>
        <mt-header fixed title="小买卖"></mt-header>
        <i></i>
    </header>
</template>

<script>
    export default {
        
    }
</script>

<style scoped>
header i{
    display: block;
    height: 40px;
}
</style>
// 编写属于自己的公共Vue组件库
import HeaderComponent from './header.vue';
import FooterComponent from './footer.vue';

// Vue插件要求提供一个install方法, 这个方法会被注入Vue
// 需要我们调用Vue的filter component directive去扩展功能
export default {
    install(Vue) {
        Vue.component('app-header', HeaderComponent);
        Vue.component('app-footer', FooterComponent);
    }
};

<template>
    <article>
        <!-- 轮播图 -->
        <mt-swipe :auto="10000">
            <mt-swipe-item v-for="item in lunbos" v-bind:key="item.url">
                <router-link v-bind:to="item.url">
                    <img v-bind:src="item.img">
                </router-link>
            </mt-swipe-item>
        </mt-swipe>
        <!-- 六宫格 -->
        <ul class="mui-table-view mui-grid-view mui-grid-9">
            <li class="mui-table-view-cell mui-media mui-col-xs-4 mui-col-sm-3">
                <router-link to="/">
                    <span class="mui-icon mui-icon-home"></span>
                    <div class="mui-media-body">首页</div>
                </router-link>
            </li>
            <li class="mui-table-view-cell mui-media mui-col-xs-4 mui-col-sm-3">
                <router-link to="/news/list">
                    <span class="mui-icon mui-icon-chat"></span>
                    <div class="mui-media-body">热点新闻</div>
                </router-link>
            </li>
            <li class="mui-table-view-cell mui-media mui-col-xs-4 mui-col-sm-3">
                <router-link to="/photo/list">
                    <span class="mui-icon mui-icon-image"></span>
                    <div class="mui-media-body">图片分享</div>
                </router-link>
            </li>
            <li class="mui-table-view-cell mui-media mui-col-xs-4 mui-col-sm-3">
                <router-link to="/">
                    <span class="mui-icon mui-icon-search"></span>
                    <div class="mui-media-body">搜索</div>
                </router-link>
            </li>
            <li class="mui-table-view-cell mui-media mui-col-xs-4 mui-col-sm-3">
                <router-link to="/">
                    <span class="mui-icon mui-icon-phone"></span>
                    <div class="mui-media-body">联系我们</div>
                </router-link>
            </li>
            <li class="mui-table-view-cell mui-media mui-col-xs-4 mui-col-sm-3">
                <router-link to="/">
                    <span class="mui-icon mui-icon-info"></span>
                    <div class="mui-media-body">关于我们</div>
                </router-link>
            </li>
        </ul>
    </article>
</template>

<script>
export default {
    data() {
        return {
            lunbos: []
        }
    },

    methods: {
        // 请求数据, 成功后把数据存储到lunbos中
        getLunbo() {
            this.axios.get(this.api.getLunbo)
            .then( rep => this.lunbos = rep.data.message );
        }
    },

    // 组件初始化完毕后, 自动调用接口获取数据渲染轮播图
    created() {
        this.getLunbo();
    }
}
</script>

<style lang="less" scoped>
@height: 260px;
article {
    height: @height;
    img {
        height: @height;
    }
}
</style>

<template>
    <div>
        新闻详情
    </div>
</template>

<script>
    export default {
        
    }
</script>

<style scoped>

</style>
<template>
    <ul>
        <li v-for="item in newsList" v-bind:key="item.id">
            <router-link v-bind="{ to: `/news/detail/${item.id}` }">
                <p>{{ item.zhaiyao }}</p>
            </router-link>
        </li>
    </ul>
</template>

<script>
export default {

    data() {
        return {
            newsList: []
        };
    },

    methods: {
        // 获取新闻列表数据
        getNewsList() {
            this.axios.get(this.api.getNL)
            .then( rsp => this.newsList = rsp.data.message )
        }
    },

    // 上来就调用接口初始化数据
    created() {
        this.getNewsList();
    }
}
</script>

<style scoped>

</style>
<template>
    <main>
        <app-header></app-header>
        <router-view></router-view>
        <app-footer></app-footer>
    </main>
</template>

<script>
export default {
    
}
</script>

<style scoped>

</style>
const domain = 'http://vue.studyit.io/api';

export default {

    // 获取轮播图的接口
    getLunbo: `${domain}/getlunbo`,

    // 新闻相关接口
    getNL: `${domain}/getnewslist`,
    getND: `${domain}/getnew/`,      // 该接口后面需要一个id

    // 图片相关接口

    // 商品相关接口

    // 评论相关接口
};

// from后面的路径, 如果含有./ ../那么就相对于当前文件找文件
// 如果没有, 那么就会去node_modules里面找对应的包
// 1.1 导入第三方包
import Vue from 'vue';
import MintUi from 'mint-ui';
import 'mint-ui/lib/style.css';
import Common from '../component/common';  // 自动找到index.js引入
import 'mui/dist/css/mui.css';
import 'mui/examples/hello-mui/css/icons-extra.css';
import axios from 'axios';  
import VueRouter from 'vue-router';

// 1.2 启用vue插件
Vue.use(MintUi);
Vue.use(Common);
Vue.use(VueRouter);

// 2.1 导入根组件
import AppComponent from '../component/App.vue';

// 2.2 导入配置
import routerConfig from '../router'    // 自动找到index.js引入
import apiConfig from './api_config.js'

// 2.3 扩展实例成员
Vue.prototype.axios = axios;   // 把axios库放置到原型, 将来其他组件直接可以拿到axios对象
Vue.prototype.api = apiConfig;

// 2.2 渲染根组件, 启动项目
new Vue({
    el: '#app',
    render(createNode) {
        return createNode(AppComponent);
    },
    router: new VueRouter(routerConfig)
});

// 这里对外导出一个路由配置对象
import HomeComponent from '../component/home/home.vue';
import NewsListComponent from '../component/news/news_list.vue';
import NewsDetailComponent from '../component/news/news_detail.vue';

export default {
    routes: [

        // 首页路由配置
        { path: "/", redirect: "/index" },
        { name: "i", path: "/index", component: HomeComponent },

        // 新闻路由配置
        { name: "nl", path: "/news/list", component: NewsListComponent },
        { name: "nd", path: "/news/detail/:id", component: NewsDetailComponent }
    ]
};

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div id="app"></div>
</body>
</html>
{
    "presets": [ "env" ],
    "plugins":["transform-runtime"]
}

# 忽略第三方包, 他们已经记录在package.json文件中了
/node_modules

# 忽略打包后的文件, 因为我们的项目核心是源代码
/dist

# 忽略隐藏文件
.* 

# 不忽略git配置文件和babel配置文件
!.gitignore
!.babelrc
const path = require('path');
const HtmlWP = require('html-webpack-plugin');
const CleanWP = require('clean-webpack-plugin');

module.exports = {
    // 打包的入口文件
    entry: path.resolve(__dirname, './src/js/main.js'),
    // 输出
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: 'bundle.js'
    },
    // 插件配置
    plugins: [
        new HtmlWP({
            template: './src/index.html',
            filename: 'index.html',
            inject: 'body'
        }),
        new CleanWP(['dist'])
    ],
    // 模块配置
    module: {

        // 配置loader规则
        rules: [

            // css
            {
                test: /\.css$/,
                use: [ 'style-loader', 'css-loader' ]
            },

            // less
            {
                test: /\.less$/,
                use: [ 'style-loader', 'css-loader', 'less-loader' ]
            },

            // html
            {
                test: /\.(html|tpl)$/,
                use: [ 'html-loader' ]
            },

            // 静态资源引用
            {
                test: /\.(png|jpeg|gif|jpg|svg|mp3|ttf)$/,
                use: [ 
                    { loader: 'url-loader', options: { limit: 10240 } } // 小于10KB的打包
                ]
            },

            // js
            {
                test: /\.js$/,
                use: [ 'babel-loader' ],
                exclude: path.resolve(__dirname, '../node_modules')
            },

            // vue
            {
                test: /\.vue$/,
                use: [ 'vue-loader' ]
            }

        ]
    }
};

3. 执行webpack-dev-server

webpack-dev-server

上一篇下一篇

猜你喜欢

热点阅读