老莫的笔记本  
  
查看: 1114|回复: 0

VUE 组件 中路由

[复制链接]

662

主题

878

帖子

5145

积分

超级版主

Rank: 8Rank: 8

积分
5145
发表于 2018-11-27 23:20:39 | 显示全部楼层 |阅读模式
本帖最后由 周大胖子 于 2018-11-28 22:34 编辑

其实和angular真像;
在大地老师的白话中,大致分为五步走
1. 安装vue-router 模块;cnpm install vue-router --save
2. 在main.js中引入 该中间件 并且使用
  1. import VueRouter from 'vue-router'
  2. Vue.use(VueRouter);
复制代码
3.配置该路由    3.1创建 组件并且引入 【就是在个Home.vue 文件和 News文件,然后在main.js中引入】
    3.2定义路由
  1. const routes = [
  2.   { path: '/home', component: Home },
  3.   { path: '/news', component: News },
复制代码
   3.3实例化路由
  1. const router = new VueRouter({
  2.   routes // short for `routes: routes`
  3. })
复制代码
4.挂载
  1. new Vue({
  2.   // 4
  3.   router,
  4.   render: h => h(App),
  5. }).$mount('#app')
复制代码

5.在App.vue中使用 该标签 规定路由区域
  1. <router-view></router-view>
复制代码

代码全部公开
main.js
  1. import Vue from 'vue'
  2. import App from './App.vue'
  3. // 3.1
  4. import Home from './components/Home'
  5. import News from './components/News'
  6. import VueRouter from 'vue-router'
  7. Vue.use(VueRouter);
  8. // 3.2
  9. const routes = [
  10.   { path: '/home', component: Home },
  11.   { path: '/news', component: News }
  12. ]
  13. const router = new VueRouter({
  14.   routes // short for `routes: routes`
  15. })
  16. Vue.config.productionTip = false
  17. new Vue({
  18.   // 4
  19.   router,
  20.   render: h => h(App),
  21. }).$mount('#app')
复制代码


app.vue
  1. <template>
  2.   <div id="app">
  3.     <hr>
  4.     <router-link to="/home">首页</router-link>
  5.     <router-link to="/news">新闻</router-link>
  6.     <router-view></router-view>
  7.   </div>
  8. </template>
  9. <script>
  10. export default {
  11.   
  12.   data(){     //业务逻辑定义的数据
  13.     return {
  14.        msg2:'哈哈哈'
  15.         
  16.     }
  17.    
  18.   }
  19.   ,methods:{        //存放VUE的方法
  20.      
  21.   }
  22.   ,name: 'app',
  23.   components: {
  24.   }
  25.   
  26. }
  27. </script>
  28. <style>
  29. #app {
  30.   font-family: 'Avenir', Helvetica, Arial, sans-serif;
  31.   -webkit-font-smoothing: antialiased;
  32.   -moz-osx-font-smoothing: grayscale;
  33.   text-align: center;
  34.   color: #2c3e50;
  35.   margin-top: 60px;
  36. }
  37. .red{
  38.   color:red;
  39. }
  40. .blue{
  41.   color:blue;
  42. }
  43. </style>
复制代码

{ path: '*', component: Home} 默认首页



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表