美文网首页
SPA单页应用(一级路由)

SPA单页应用(一级路由)

作者: 蜜思1013 | 来源:发表于2019-04-17 21:40 被阅读0次

    一个vue的单页应用(一级路由)的脚手架程序构建

    1.命令行先进入某个目录如D:\VueStudy
    2.创建项目: vue init webpack demo
    3.进入demo目录安装依赖:npm install
    4.修改config目录下index.js的dev端口为80
    5.运行:npm run dev,打开http://localhost,看到Vue主页logo即成功

    App.vue

    <template>
    <div id="app" class="container">
    <div class="header">
    <router-link to="/" class="nav-item">首页</router-link>
    <router-link to="/message" class="nav-item">消息</router-link>
    </div>
    <div class="content">
    <router-view></router-view>
    </div>
    </div>
    </template>
    <script>
    export default {
    name: 'App'
    }
    </script>
    <style>
    .container{
    width: 80%;
    margin: 0 auto;
    height: 400px;
    }
    .header{
    display: flex;
    height: 100px;
    background-color: darkcyan;
    font-size: 20px;
    padding-left: 20px;
    align-items: center;
    }
    .nav-item{
    margin-right: 20px;
    }
    .content{
    background-color: #2c3e50;
    color: #ffffff;
    }
    </style>
    

    index.vue

    <template>
    <div class="container">
    <h2>首页</h2>
    {{msg}}
    </div>
    </template>
    <script>
    export default{
    name:'Index',
    data(){
    return{
    msg:'首页'
    };
    }
    };
    </script>
    <style scoped>
    </style>
    

    Message.vue

    <template>
    <div class="container">
    <h2>消息</h2>
    {{ msg }}
    </div>
    </template>
    <script>
    export default {
    name: 'Index',
    data() {
    return {
    msg: '消息'
    };
    }
    };
    </script>
    <style scoped></style>
    

    index.js

    import Vue from 'vue'
    import Router from 'vue-router'
    Vue.use(Router)
    export default new Router({
    mode:"history",
    //使上面的/后面没有#号如:
    routes: [
    {
    path: '/',
    name: 'Index',
    component: resolve=>require(['../components/Index.vue'],resolve)
    },
    {
    path: '/message',
    name: 'Message',
    component: resolve=>require(['../components/Message.vue'],resolve)
    },
    ]
    })
    

    相关文章

      网友评论

          本文标题:SPA单页应用(一级路由)

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