2018-8-30-router的自我反思与总结三

一、main.js代码

//引入一堆
import Vue from 'vue';
//主体
import App from './components/app.vue';

//引入
import VueResource from 'vue-resource';
//安装插件,插件都是挂载属性
Vue.use(VueResource);
//未来通过this.$http
//  Vue是 所有实例对象的构造函数
//  Vue.protptype.$http ->   实例(this)就可以.$http

//new Vue 启动
new Vue({
    el: '#app',
    render: c => c(App),
})

二、app.vue 代码

<template>
    <div>
        {{data}}
    </div>
</template>
<script>
    export default {
        data(){
            return {
                data:[]
            }
        },created(){
            // console.log(this.$http);
            // this.$http.get('http://182.254.146.100:8899/api/getlunbo')
            // .then(res=>{
            //     this.data = res.body.message;
            // },err=>{
            //     console.log(err)
            // })
            
            //post请求
            this.$http.post('http://182.254.146.100:8899/api/postcomment/300',{
                content:'传智27期,霸气的一批'
            },{
                emulateJSON:true
            })
            .then(res=>{
                this.data = res.body.message;
            },err=>{
                console.log(err);
            })

        }
    }
</script>
<style scoped>
    .h{
        height: 100px;
        background-color: yellowgreen;
    }
    .b{
        height: 100px;
        background-color: skyblue;
    }
    .f{
        height: 100px;
        background-color: hotpink;
    }
</style>

*****************************************

一、引入 main.js

//引入一堆
import Vue from 'vue';
import VueRouter from 'vue-router';

//主体
import App from './components/app.vue';
//路由切换页面
import header from './components/header.vue'
import footer from './components/footer.vue'
import Music from './components/music.vue'
import Oumei from './components/oumei.vue'
import Guochan from './components/guochan.vue'

//注册全局组件
Vue.component('headerVue', header);
Vue.component('footerVue', footer);

//安装插件
Vue.use(VueRouter); //挂载属性

//创建路由对象并配置路由规则
let router = new VueRouter({
    //routes
    routes: [{
            path: '/',
            redirect: { name: 'music' },
        },
        {
            name: 'music',
            path: '/music',
            component: Music,
            children: [

                //-> 这里很灵活,如果你写上/xxx  就是绝对路径, /oumei
                //如果你不写/  ,那么就是相对路径 /music/oumei
                { name: 'music_oumei', path: 'oumei', component: Oumei },
                //标识一下,当前路由之间的关系,格式不是必须的
                { name: 'music_guochan', path: 'guochan', component: Guochan }
            ]
        }

    ]
});

//new Vue 启动
new Vue({
    el: '#app',
    //让vue知道我们的路由规则
    router, //可以简写router
    render: c => c(App),
})

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值